如何替换单词后的数字字符计数


How to replace the word after a number of character count?

我需要一些PHP方面的帮助,因为PHP会忽略500个字符后的单词。

这是我的PHP代码:

if(str_word_count($rsa['content']) > 500){
}

我将把这个内容推送到一个数组中,数组已经在一个循环中了。我只是不知道如何忽略500个字符后的单词计数。

有人能帮我解决吗?提前感谢。

简单。使用substr

$content = substr($rsa['content'], 0, 500);

其工作方式是substr是函数,$rsa['content']是字符串值,0substr应该开始的地方&500是您希望返回的字符串长度。

也就是说,当你打算使用strlen时,你似乎使用了计算单词的str_word_count,因为它计算字符。知道了这一点,我会这样实施它。T:

// Assign the value of `$rsa['content']` to `$content`.
$content = $rsa['content'];
// Set a `$content_length` to save yourself typing & logic headaches.
$content_length = 500;
// Check the string length & act on it.
if (strlen($content) > $content_length) {
  $content = substr($content, 0, $content_length);
}

但你可能会通过使用三元运算符来让它变得更光滑,比如

// Check the string length & act on it.
$content = strlen($content) > $content_length ? substr($content, 0, $content_length) : $content;