function limit_words($words, $limit, $append = ' …') {
// Add 1 to the specified limit becuase arrays start at 0
$limit = $limit+1;
// Store each individual word as an array element
// Up to the limit
$words = explode(' ', $words, $limit);
// Shorten the array by 1 because that final element will be the sum of all the words after the limit
array_pop($words);
// Implode the array for output, and append an ellipse
$words = implode(' ', $words) . $append;
// Return the result
return $words;
}
<?php
function substr_sentence($string, $start=0, $limit=10, $max_char = 300)
{
/* This functions cuts a long string in sentences.
*
* substr_sentence($string, $start, $limit);
* $string = 'A example. By someone that loves PHP. Do you? We do!';
* $start = 0; // we would start at the beginning
* $limit = 10; // so, we get 10 sentences (not 10 words or characters!)
*
* It's not as substr()) in single characters.
* It's not as substr_words() in single words.
*
* No more broken lines in a story. The story/article must go on!
*
* Written by Eddy Erkelens "Zunflappie"
* Published on www.mastercode.nl
* May be free used and adapted
*
*/
// list of sentences-ends. All sentences ends with one of these. For PHP, add the ;
$end_characters = array(
'.',
'?',
'!'
);
// put $string in array $parts, necessary evil
$parts = array($string);
// foreach interpunctation-mark we will do this loop
foreach($end_characters as $end_character)
{
// go thru each part of the sentences we already have
foreach($parts as $part)
{
// make array with the new sentences
$sentences[] = explode($end_character, $part);
}
// unfortunately explode() removes the end character itself. So, place it back
foreach($sentences as $sentence)
{
// some strange stuff
foreach($sentence as $real_sentence)
{
// empty sentence we do not want
if($real_sentence != '')
{
// if there is already an end-character, dont place another one
if(in_array(substr($real_sentence, -1, 1), $end_characters))
{
// store for next round
$next[] = trim($real_sentence);
}
else
{
// store for next round
$next[] = trim($real_sentence).$end_character;
}
}
}
}
// store for next round
$parts = $next;
// unset the remaining and useless stuff
unset($sentences, $sentence, $next);
}
// check for max-char-length
$total_chars = 0;
$sentence_nr = 0;
$sentences = array();
// walk thru each member of $part
foreach($parts as $part)
{
// count the string-lenght and add this to $total_chars
$total_chars += strlen($part);
// if $total-chars is higher then max-char, dont add this sentences!
if($total_chars
// list of sentences-ends. All sentences ends with one of these. For PHP, add the ;
$end_characters = array(
'. ',
'? ',
'! '
);
// put $string in array $parts, necessary evil
$parts = array($string);
// foreach interpunctation-mark we will do this loop
foreach($end_characters as $end_character)
{
// go thru each part of the sentences we already have
foreach($parts as $part)
{
// make array with the new sentences
$sentences[] = explode($end_character, $part);
}
// unfortunately explode() removes the end character itself. So, place it back
foreach($sentences as $sentence)
{
// some strange stuff
foreach($sentence as $real_sentence)
{
// empty sentence we do not want
if($real_sentence != '')
{
// if there is already an end-character, dont place another one
if(in_array(substr($real_sentence, -1, 1), $end_characters))
{
// store for next round
$next[] = trim($real_sentence);
}
else
{
// store for next round and add the removed character
$next[] = trim($real_sentence).$end_character;
}
}
}
}
// store for next round
$parts = $next;
// unset the remaining and useless stuff
unset($sentences, $sentence, $next);
}
// check for max-char-length
$total_chars = 0;
$sentence_nr = 0;
$sentences = array();
// walk thru each member of $part
foreach($parts as $part)
{
// count the string-lenght and add this to $total_chars
$total_chars += strlen($part);
// if $total-chars not already higher then max-char, add this sentences!
if($total_chars
哪种方法在高流量网站上更好?
除了单词之外,这适用于句子。
所以不再有半句了
@ Eddy Erkelens。你的解决方案非常有趣。你能否发布函数的完整代码,因为它在if($total_chars…部分被截断了。
这是它
有点晚了,但我确实需要再次使用该函数,为自己服务
…
<?php
function substr_sentence($string, $start=0, $limit=10, $max_char = 600)
{
/* 此函数将长字符串截断为句子。
*
* substr_sentence($string, $start, $limit);
* $string = '一个例子。由一个热爱PHP的人编写。你呢?我们会的!';
* $start = 0; // 我们将从开头开始
* $limit = 10; // 所以,我们得到10个句子(而不是10个单词或字符!)
*
* 它不像substr())那样以单个字符为单位。
* 它不像substr_words()那样以单个单词为单位。
*
* 故事中不再出现断行。故事/文章必须继续!
*
* 由Eddy Erkelens "Zunflappie"编写
* 发布在http://www.mastercode.nl
* 可以免费使用和修改
*
*/
…
以及最后的部分… 分开。
…
// 遍历$part的每个成员
foreach($parts as $part)
{
// 计算字符串长度并将其添加到$total_chars中
$total_chars += strlen($part);
…
糟糕的错误…
它在ARROW_LEFT上被截断了..
所以:codepad来救援。抱歉,克里斯。
链接:http://codepad.org/AqIL9V4N
以及一个substr_words():http://codepad.org/jCLTm1zJ
以及一个句子计数器:http://codepad.org/o9I3zdI9
总而言之,你可以计算长文本的句子数。
然后将其分成两半(使用ceil()或abs())。
使用substr_sentences()将其切片,然后大功告成。
你好,
我想在200个字符后截断句子,然后添加“阅读更多”链接。
你能帮我一下吗?
我注意到此函数中存在一个错误。当我的字符串被返回时,每个句子的结尾都是“.!?”,因为foreach ($end_characters)循环始终测试$real_sentence中是否存在结束字符,并且当它添加结束字符时,该字符串不会更新,只有存储的字符串会被更新。因此,每次循环遍历end_characters时,字符串都不包含字符,因此会添加一个。
Matt – 我遇到了同样的问题,该函数在每个句子后面都留下了‘.!?’。原因是在$end_characters数组中添加了不必要的空格。Eddy的codepad中的第24行:http://codepad.org/AqIL9V4N应更改为(注意每个变量中没有空格)
希望这对某人有所帮助!
KB