【问题标题】:How to add a "..." after a particular word/character limit in PHP?如何在 PHP 中的特定单词/字符限制后添加“...”?
【发布时间】:2019-05-08 15:48:02
【问题描述】:

我正在编写如下所示的 php 代码:

<div class="case-breaking__content">
    <p><?php echo the_title(); ?></p>
</div>
      

以上php代码返回如下内容:

absv shss xcnx shss hshhs shhsw shshs hsnna hssnnss hssns snnss nnshs sjjjjsjsj nsnnnsns jjsnss snsnns nsnns 

我想要的是,在特定的字数限制之后它应该显示......让我们说在 8 个字之后它应该是这样的;

absv shss xcnx shss hshhs shhsw shshs hsnna...

我尝试了以下方式,但它显示了所有单词:

<div class="case-breaking__content">
<p><?php $out = strlen(the_title()) > 50 ? substr(the_title(),0,50)."..." : the_title(); ?></p>
</div>

【问题讨论】:

标签: php wordpress string split strlen


【解决方案1】:

如果您愿意,您可以简单地在空格上执行preg_split,然后将数组连接回字符串,并带有限制数:

$str = "absv shss xcnx shss hshhs shhsw shshs hsnna hssnnss hssns snnss nnshs sjjjjsjsj nsnnnsns jjsnss snsnns nsnns
";

$arr = preg_split('/(\s)/m', $str);
$limit = 8;
$title = '';
foreach ($arr as $key => $value) {
    if ($key < $limit) {
        $title .= $value . " ";
    } else {
        $title .= "...";
        break;
    }
}

var_dump($title);

输出

string(47) "absv shss xcnx shss hshhs shhsw shshs hsnna ..."

您还可以添加一个if 语句,以防单词数少于您想要的限制,以便打破循环,可能类似于:

$str = "absv shss xcnx shss hshhs";
// $str = "absv shss xcnx shss hshhs shhsw shshs hsnna hssnnss hssns snnss nnshs sjjjjsjsj nsnnnsns jjsnss snsnns nsnns";

$arr = preg_split('/(\s)/m', $str);
$limit = 8;
$title = '';
foreach ($arr as $key => $value) {
    if (sizeof($arr) < $limit - 1) {
        $title .= $str . " ...";
        break;
    }
    if ($key < $limit) {
        $title .= $value . " ";
    } else {
        $title .= "...";
        break;
    }
}

var_dump($title);

输出

string(29) "absv shss xcnx shss hshhs ..."

实施

如果您希望实现此代码,这可能会起作用:

$arr = preg_split('/(\s)/m', get_the_title());
$limit = 8; // number of words limit
$title = '';
foreach ($arr as $key => $value) {
    if (sizeof($arr) < $limit - 1) {
        $title .= get_the_title() . " ...";
        break;
    }
    if ($key < $limit) {
        $title .= $value . " ";
    } else {
        $title .= "...";
        break;
    }
}

$out = '<div class="case-breaking__content">';
$out .= '<p>';
$out .= $title;
$out .= '</p>';
$out .= '</div>';

echo $out;

【讨论】:

  • \s 周围的捕获括号没有任何好处。如果两个或多个空格连续出现,在\s 之后不使用+ 可能会计算“幻词”(空字符串)。 m 模式没有用,因为模式中没有锚(例如^$)。此解决方案在不需要时添加三个点(不需要截断字符串)证明:3v4l.org/DOYXm
【解决方案2】:

the_title() 回显标题而不是返回它。如果您正在对其执行操作,那么您想使用get_the_title()

<div class="case-breaking__content">
<p><?php echo strlen(get_the_title()) > 50 ? substr(get_the_title(), 0, 50) . "..." : get_the_title(); ?></p>
</div>

也就是说,如果您使用 WordPress,最简单的选择是使用内置函数 wp_trim_words() https://codex.wordpress.org/Function_Reference/wp_trim_words

所以你的情况

<div class="case-breaking__content">
<p><?php echo wp_trim_words( get_the_title(), 8, '...') ?></p>
</div>

【讨论】:

    【解决方案3】:
    <div class="case-breaking__content">
        <?php $words = explode(" ", the_title()); ?>
        <p><?php echo count($words) <= 8 ? the_title() : implode(" ", array_slice($words, 0, 8)) . "..."; ?></p>
    </div>
    

    普通的 PHP 解决方案,不需要比这更复杂。

    【讨论】:

    • 仅供参考,the_title() 的默认行为将 echo 标题。最好打电话给get_the_title()。请看我的回答。如果您添加一些关于您的代码如何工作的解释,您的答案会更好。
    【解决方案4】:

    我不使用 WordPress,但它的 source code for wp_trim_words() 看起来比我喜欢的要麻烦。

    the_title() 的默认行为是echo 字符串。在这种情况下,您需要return 字符串,以便在回显之前对其进行变异。这是通过将其第三个参数设置为false 来实现的。 https://developer.wordpress.org/reference/functions/the_title/

    或者更简单地拨打get_the_title()

    要有条件地应用字数限制,一个 preg_replace() 电话就足够了。

    从逻辑上讲,除非超出字数限制,否则添加省略号是没有意义的,我下面的模式检查 8 组零个或多个空格,后跟一个或多个非空格。如果有 8 个“单词”并且至少要删除一个字符,则\K“忘记”之前匹配的 8 个单词,然后将输入字符串的其余部分替换为三个点。

    代码:(Demo with hardcoded sample strings)

    $wordLimit = 8;
    echo preg_replace('/(?:\s*\S++){' . $wordLimit . '}\K.+/', '...', get_the_title());
    

    \S++所有格的一个或多个量词强制非空白字符的贪婪匹配 - 这确保了最终单词的所有都匹配。

    【讨论】:

      猜你喜欢
      • 2016-09-19
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      • 2020-04-13
      • 1970-01-01
      相关资源
      最近更新 更多