PHP中的子字符串函数不起作用


sub string function in PHP not working

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

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

但它不起作用。它显示全部内容。

试试这个代码

$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;
}

使用以下代码可以帮助您

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

我使用这个代码,现在它正在中工作

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

缺少"0"。