gpt4 book ai didi

php - 剪掉10个字后的内容

转载 作者:可可西里 更新时间:2023-11-01 13:04:53 28 4
gpt4 key购买 nike

Possible Duplicate:
How to select first 10 words of a sentence?

我想显示内容词的 10 个词而不是字符

$string = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.';

结果应该是
“Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare”

最佳答案

试试这个功能:

function shorten_string($string, $wordsreturned)
{
$retval = $string;
$string = preg_replace('/(?<=\S,)(?=\S)/', ' ', $string);
$string = str_replace("\n", " ", $string);
$array = explode(" ", $string);
if (count($array)<=$wordsreturned)
{
$retval = $string;
}
else
{
array_splice($array, $wordsreturned);
$retval = implode(" ", $array)." ...";
}
return $retval;
}

在你的文字上,像这样:

$string = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.';
$firsttenwords = shorten_string($string, 10);

来自 here .

更新:现在它既符合空间要求,也符合换行要求。

关于php - 剪掉10个字后的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12444945/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com