PHP中的子字符串函数无法正常工作

时间:2014-09-22 10:47:32

标签: php wordpress substring substr

我想使用PHP子串函数在wordpress中显示限制内容。我正在使用此代码

<?php $content = get_the_content(); echo substr($content, 50); ?>

但它不起作用。它显示完整的内容。

3 个答案:

答案 0 :(得分:4)

试试此代码

$content = get_the_content();
echo substr($content, 0, 50);

或者更好地检查长度:

$content = get_the_content();

if (strlen($content) > 50)
    $content = substr($content, 0, 50);

echo $content;

在一个功能中:

echo mytruncate(get_the_content());


function mytruncate($text, $length=50) {
    if (strlen($text) > $length)
        return substr($text, 0, $length);
    return $text;
}

答案 1 :(得分:0)

使用以下代码可以帮助您

<?php
$content = get_the_content();
echo substr($content, 0, 50);
?>

答案 2 :(得分:0)

我使用此代码,现在正在使用

<?php $content = get_the_content(); echo substr($content, 0, 50); ?>

'0'丢失了。