php 剪切10个字后的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12444945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cut the content after 10 words
提问by Déjà Bond
Possible Duplicate:
How to select first 10 words of a sentence?
可能的重复:
如何选择句子的前 10 个单词?
I want to show 10 words of the content words not characters
我想显示内容单词的 10 个单词而不是字符
$string = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.';
should the result to be
"Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare"
结果应该是
“Lorem ipsum dolor sat amet,consectetur adipiscing elit. Mauris ornare”
回答by Lucas
Try this function:
试试这个功能:
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;
}
On your text, so like this:
在你的文字上,像这样:
$string = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.';
$firsttenwords = shorten_string($string, 10);
From here.
从这里开始。
UPDATE:Now it's space-compliant, and also new-line compliant.
更新:现在它是空间兼容的,也兼容换行符。
回答by Pebbl
This version will work no matter what kind of "space" you use between words and can be easily extended to handle other characters... it currently supports any white space character plus , . ; ? !
无论您在单词之间使用哪种“空格”,此版本都可以使用,并且可以轻松扩展以处理其他字符……它目前支持任何空白字符加 , 。; ? !
function getSnippet( $str, $wordCount = 10 ) {
return implode(
'',
array_slice(
preg_split(
'/([\s,\.;\?\!]+)/',
$str,
$wordCount*2+1,
PREG_SPLIT_DELIM_CAPTURE
),
0,
$wordCount*2-1
)
);
}
For those who shouldmight prefer the original formatting :)
对于那些 应该可能更喜欢原始格式:)
function getSnippet( $str, $wordCount = 10 ) {
return implode( '', array_slice( preg_split('/([\s,\.;\?\!]+)/', $str, $wordCount*2+1, PREG_SPLIT_DELIM_CAPTURE), 0, $wordCount*2-1 ) );
}
回答by Rubin Porwal
We can retrieve the words in a string using str_word_count function .
我们可以使用 str_word_count 函数检索字符串中的单词。
For more description about the function please refer the below link
有关该功能的更多说明,请参阅以下链接
http://php.net/manual/en/function.str-word-count.php
http://php.net/manual/en/function.str-word-count.php
For displaying only 10 words in the string please refer the below code snippet
要在字符串中仅显示 10 个单词,请参阅以下代码片段
$str='Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.';
$words=str_word_count($str,true);
$a=array_slice($words,10);
$s=join('',$a);
echo $s;
回答by alfasin
Try:
尝试:
$str = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.';
$arr = explode(" ", str_replace(",", ", ", $str));
for ($index = 0; $index < 10; $index++) {
echo $arr[$index]. " ";
}
Output:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ornare
输出:
Lorem ipsum dolor sat amet,consectetur adipiscing elit。毛里斯·奥纳雷
回答by Justin John
Try like this
像这样尝试
$str = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.';
$arr = explode(" ", str_replace(",",", ",$str), 10);
echo implode(" ", $arr);

