当有 space 时,使介绍文章中断返回的方法
Method to make intro article break the returned when there's a space
我想为文章介绍制作一种方法,该方法会在 space、逗号、句号、分号、问号或感叹号处打断返回的文本。我被困在这段代码中:
function getIntro($count = 200) {
return substr(strip_tags($this->content), 0, $count) . '...';
}
请告诉我如何使用我的函数做到这一点?
function splitAtBlank($string, $tryToSplitAt){
$string = substr($string, 0, $tryToSplitAt);
$i = strlen($string);
$i--;
while($string[$i] != " " && $i > 0){
$i--;
}
return substr($string, 0, $i);
}
那你就可以简单地使用它了:
echo splitAtBlank("Your string", 8);
为了您的目的:
function getIntro($count = 200) {
$string = substr(strip_tags($this->content), 0, $count);
$i = strlen($string);
$i--;
while($string[$i] != " " && $i > 0){
$i--;
}
return substr($string, 0, $i) . '...';
}
还有另一个处理多个分隔符的版本:
function getIntro($count = 200){
$chars = array(' ', '.', ',', '!', '?');
$string = substr(strip_tags($this->content), 0, $count);
$i = strlen($string) - 1;
while(!in_array($string[$i], $chars) && $i > 0){
$i--;
}
return substr($string, 0, $i) . '...';
}
如果您的文章中有一些 HTML 标签,您应该在拆分之前使用 strip_tags()
。
如果你需要处理标签 open/closed 状态评论这个 post.
如果要查找位置 200 之前最近的 space、逗号、句点、分号、问号或感叹号,则必须查找所有这些并获得可用文本的最大部分.
因此,如果您的文字是:
$text='"It is a long established fact that a reader will be distracted
by the readable content of a page when looking at its layout? The
point of using Lorem Ipsum is that it has a more-or-less normal
distribution", i wish my function will make intro stop at word normal,
rather than stop at word distribu and then cut,it';
//cut the string at 200
$cut_text=substr($text,0,200);
//look for all these characters.
//strripos returns the position of the last occurrance of the character
$needles=array(' ','.',',','?','!',';');
foreach($needles as $needle){
$pos[]=strripos($cut_text,$needle);
}
//sort the array to get the biggest 'position'. The biggest is $pos[0]
rsort($pos);
//look if there is found a match, if not: just take the 200 characters
if(empty($pos[0])){$result=$cut_text;}
else{
$result=substr($cut_text,0,$pos[0]);
}
echo $result;
在示例中,它在
之后立即剪切文本
more-or-less normal
我想为文章介绍制作一种方法,该方法会在 space、逗号、句号、分号、问号或感叹号处打断返回的文本。我被困在这段代码中:
function getIntro($count = 200) {
return substr(strip_tags($this->content), 0, $count) . '...';
}
请告诉我如何使用我的函数做到这一点?
function splitAtBlank($string, $tryToSplitAt){
$string = substr($string, 0, $tryToSplitAt);
$i = strlen($string);
$i--;
while($string[$i] != " " && $i > 0){
$i--;
}
return substr($string, 0, $i);
}
那你就可以简单地使用它了:
echo splitAtBlank("Your string", 8);
为了您的目的:
function getIntro($count = 200) {
$string = substr(strip_tags($this->content), 0, $count);
$i = strlen($string);
$i--;
while($string[$i] != " " && $i > 0){
$i--;
}
return substr($string, 0, $i) . '...';
}
还有另一个处理多个分隔符的版本:
function getIntro($count = 200){
$chars = array(' ', '.', ',', '!', '?');
$string = substr(strip_tags($this->content), 0, $count);
$i = strlen($string) - 1;
while(!in_array($string[$i], $chars) && $i > 0){
$i--;
}
return substr($string, 0, $i) . '...';
}
如果您的文章中有一些 HTML 标签,您应该在拆分之前使用 strip_tags()
。
如果你需要处理标签 open/closed 状态评论这个 post.
如果要查找位置 200 之前最近的 space、逗号、句点、分号、问号或感叹号,则必须查找所有这些并获得可用文本的最大部分.
因此,如果您的文字是:
$text='"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout? The point of using Lorem Ipsum is that it has a more-or-less normal distribution", i wish my function will make intro stop at word normal, rather than stop at word distribu and then cut,it';
//cut the string at 200
$cut_text=substr($text,0,200);
//look for all these characters.
//strripos returns the position of the last occurrance of the character
$needles=array(' ','.',',','?','!',';');
foreach($needles as $needle){
$pos[]=strripos($cut_text,$needle);
}
//sort the array to get the biggest 'position'. The biggest is $pos[0]
rsort($pos);
//look if there is found a match, if not: just take the 200 characters
if(empty($pos[0])){$result=$cut_text;}
else{
$result=substr($cut_text,0,$pos[0]);
}
echo $result;
在示例中,它在
之后立即剪切文本more-or-less normal