如何在 PHP 中截断 20 个单词后的字符串?
最佳答案
function limit_text($text, $limit) {
if (str_word_count($text, 0) > $limit) {
$words = str_word_count($text, 2);
$pos = array_keys($words);
$text = substr($text, 0, $pos[$limit]) . '...';
}
return $text;
}
echo limit_text('Hello here is a long sentence that will be truncated by the', 5);
输出:
Hello here is a long ...
关于php - 如何将字符串截断为 PHP 中的前 20 个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/965235/