【问题标题】:sub string function in PHP not workingPHP中的子字符串函数不起作用
【发布时间】:2014-11-16 07:28:13
【问题描述】:

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

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

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

【问题讨论】:

标签: php wordpress substring substr


【解决方案1】:

试试这个代码

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

【讨论】:

  • @kingkero 哈哈 :) noIdidNotDoItOnPorpouse,JustHurry
【解决方案2】:

我使用这个代码,现在它可以工作了

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

“0”丢失。

【讨论】:

    【解决方案3】:

    使用下面的代码可能会对您有所帮助

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

    【讨论】:

      猜你喜欢
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多