Php 如何在空格后拆分内容
我只是个PHP新手 现在我的拆分内容功能有问题, 我有一根像这样的线:Php 如何在空格后拆分内容,php,Php,我只是个PHP新手 现在我的拆分内容功能有问题, 我有一根像这样的线: $tring = 'World Cup 2014 draw: England's chances of landing tough group rise'; 我的内容代码: if(strlen($string)<$width){ return $string; } $string = substr($string,0,$width); $string = $string.'.
$tring = 'World Cup 2014 draw: England's chances of landing tough group rise';
我的内容代码:
if(strlen($string)<$width){
return $string;
}
$string = substr($string,0,$width);
$string = $string.'...';
return $string;
它看起来像这样:
$tring = 'World Cup 2014 draw: England's chances of landing tough group rise';
2014年世界杯抽签:英格兰队……的机会
现在我希望我的字符串如下所示:
$tring = 'World Cup 2014 draw: England's chances of landing tough group rise';
2014年世界杯抽签:英格兰队登陆的机会…我该怎么办?感谢您的帮助这将以
$width
字符分割内容,但如果它落在一个单词上,它将匹配到单词的结尾(因此,如果在单词中间,它将真正是$width+[num chars,直到单词结尾)。“单词”被定义为字母、数字、下划线、连字符或单引号。这将解释如下内容“England”或“England's”或“pre-text”作为一个完整的词
$width = 20;
$text = "World Cup 2014 draw: England's chances of landi-ng tough group rise";
preg_match("~^.{".($width-1)."}([\w'-]+)?~i",$text,$m);
$newtext = $m[0].'...';
echo $newtext.'<br/>';
$width=20;
$text=“2014年世界杯抽签:英格兰队在艰难的小组赛中获胜的机会”;
preg_匹配(“~^.{”。($width-1)。”}([\w'-]+)?~i“,$text,$m);
$newtext=$m[0]。“…”;
回显$newtext.“
”;
这将以$width
字符分割内容,但如果它落在一个单词上,它将匹配到单词的结尾(因此,如果在单词中间,它将真正是$width+[num chars,直到单词的结尾)。“单词”被定义为字母、数字、下划线、连字符或单引号。这将解释“英格兰”或“英格兰”之类的内容或“前文本”作为一个完整的词
$width = 20;
$text = "World Cup 2014 draw: England's chances of landi-ng tough group rise";
preg_match("~^.{".($width-1)."}([\w'-]+)?~i",$text,$m);
$newtext = $m[0].'...';
echo $newtext.'<br/>';
$width=20;
$text=“2014年世界杯抽签:英格兰队在艰难的小组赛中获胜的机会”;
preg_匹配(“~^.{”。($width-1)。”}([\w'-]+)?~i“,$text,$m);
$newtext=$m[0]。“…”;
回显$newtext.“
”;
也许这个函数会对你有所帮助。它可以帮助你截断内容或字符串,而不让单词在中间剪切。它还考虑HTML标签。
function truncate_content( $text, $length = 100, $ending = '...', $exact = false, $considerHtml = true)
{
if ($considerHtml)
{
// if the plain text is shorter than the maximum length, return the whole text
if (strlen(preg_replace('/<.*?>/', '', $text)) <= $length)
{
return $text;
}
// splits all html-tags to scanable lines
preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
$total_length = strlen($ending);
$open_tags = array();
$truncate = '';
foreach ($lines as $line_matchings)
{
// if there is any html-tag in this line, handle it and add it (uncounted) to the output
if (!empty($line_matchings[1]))
{
// if it's an "empty element" with or without xhtml-conform closing slash
if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1]))
{
// do nothing
// if tag is a closing tag
}
else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings))
{
// delete tag from $open_tags list
$pos = array_search($tag_matchings[1], $open_tags);
if ($pos !== false)
{
unset($open_tags[$pos]);
}
// if tag is an opening tag
}
else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings))
{
// add tag to the beginning of $open_tags list
array_unshift($open_tags, strtolower($tag_matchings[1]));
}
// add html-tag to $truncate'd text
$truncate .= $line_matchings[1];
}
// calculate the length of the plain text part of the line; handle entities as one character
$content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));
if ($total_length+$content_length> $length)
{
// the number of characters which are left
$left = $length - $total_length;
$entities_length = 0;
// search for html entities
if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) {
// calculate the real length of all entities in the legal range
foreach ($entities[0] as $entity)
{
if ($entity[1]+1-$entities_length <= $left)
{
$left--;
$entities_length += strlen($entity[0]);
}
else
{
// no more characters left
break;
}
}
}
$truncate .= substr($line_matchings[2], 0, $left+$entities_length);
// maximum lenght is reached, so get off the loop
break;
}
else
{
$truncate .= $line_matchings[2];
$total_length += $content_length;
}
// if the maximum length is reached, get off the loop
if($total_length>= $length) {
break;
}
}
}
else
{
if (strlen($text) <= $length)
{
return $text;
}
else
{
$truncate = substr($text, 0, $length - strlen($ending));
}
}
// if the words shouldn't be cut in the middle...
if (!$exact)
{
// ...search the last occurance of a space...
$spacepos = strrpos($truncate, ' ');
if (isset($spacepos))
{
// ...and cut the text in this position
$truncate = substr($truncate, 0, $spacepos);
}
}
// add the defined ending to the text
$truncate .= $ending;
if($considerHtml)
{
// close all unclosed html-tags
foreach ($open_tags as $tag)
{
$truncate .= '</' . $tag . '>';
}
}
return $truncate;
}
希望这有助于你 这个函数可以帮助你。它可以帮助你截断内容或字符串,而不让单词在中间剪切。它还考虑HTML标签。
function truncate_content( $text, $length = 100, $ending = '...', $exact = false, $considerHtml = true)
{
if ($considerHtml)
{
// if the plain text is shorter than the maximum length, return the whole text
if (strlen(preg_replace('/<.*?>/', '', $text)) <= $length)
{
return $text;
}
// splits all html-tags to scanable lines
preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
$total_length = strlen($ending);
$open_tags = array();
$truncate = '';
foreach ($lines as $line_matchings)
{
// if there is any html-tag in this line, handle it and add it (uncounted) to the output
if (!empty($line_matchings[1]))
{
// if it's an "empty element" with or without xhtml-conform closing slash
if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1]))
{
// do nothing
// if tag is a closing tag
}
else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings))
{
// delete tag from $open_tags list
$pos = array_search($tag_matchings[1], $open_tags);
if ($pos !== false)
{
unset($open_tags[$pos]);
}
// if tag is an opening tag
}
else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings))
{
// add tag to the beginning of $open_tags list
array_unshift($open_tags, strtolower($tag_matchings[1]));
}
// add html-tag to $truncate'd text
$truncate .= $line_matchings[1];
}
// calculate the length of the plain text part of the line; handle entities as one character
$content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));
if ($total_length+$content_length> $length)
{
// the number of characters which are left
$left = $length - $total_length;
$entities_length = 0;
// search for html entities
if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) {
// calculate the real length of all entities in the legal range
foreach ($entities[0] as $entity)
{
if ($entity[1]+1-$entities_length <= $left)
{
$left--;
$entities_length += strlen($entity[0]);
}
else
{
// no more characters left
break;
}
}
}
$truncate .= substr($line_matchings[2], 0, $left+$entities_length);
// maximum lenght is reached, so get off the loop
break;
}
else
{
$truncate .= $line_matchings[2];
$total_length += $content_length;
}
// if the maximum length is reached, get off the loop
if($total_length>= $length) {
break;
}
}
}
else
{
if (strlen($text) <= $length)
{
return $text;
}
else
{
$truncate = substr($text, 0, $length - strlen($ending));
}
}
// if the words shouldn't be cut in the middle...
if (!$exact)
{
// ...search the last occurance of a space...
$spacepos = strrpos($truncate, ' ');
if (isset($spacepos))
{
// ...and cut the text in this position
$truncate = substr($truncate, 0, $spacepos);
}
}
// add the defined ending to the text
$truncate .= $ending;
if($considerHtml)
{
// close all unclosed html-tags
foreach ($open_tags as $tag)
{
$truncate .= '</' . $tag . '>';
}
}
return $truncate;
}
希望这对您有所帮助结果似乎与(隐式)说明不一致。听起来您希望将字符串长度限制在$width以内,对吗?并且拼写的落地符将超过$width。因此,就我所知,第二个是正确的。很抱歉,我的英语不好,我不太明白您的意思:(,我的目的是在一个空间后拆分内容,如下所示“…着陆的机会…”。但当我使用$string=substr($string,0,strrpos($string,,)”)时,它在一个空间前拆分字符串,如下所示“…着陆的机会…”如果内容长度大于宽度,您似乎需要拆分内容。是这样吗?而且您不希望在中间剪切单词,结果似乎与(隐式)不一致说明。听起来你想把字符串限制在不超过$width-length,对吗?而且拼写出来的登陆将超过$width。所以第二个就我看来是正确的。对不起,我的英语不好,我不太明白你说的:(,我的目的是在一个空格后拆分内容,看起来像这样“…登陆的机会…”.但是当我使用$string=substr($string,0,strrpos($string,,))时,它会在一个空格前拆分字符串,如下所示“…可能性…”如果内容长度大于宽度,你似乎需要拆分内容。是吗?你不想在中间切字谢谢你,萨巴里。那太棒了:D。对不起,我没有足够的声誉来投票支持你的答案:D@user3064132没关系。如果对您有帮助,Mark已解决:-)谢谢你,萨巴里。那太棒了:D。对不起,我没有足够的声望来投票支持你的答案:D@user3064132没关系。马克解决了,如果它对你有帮助:-)@user3064132欢迎!如果这对你有用,那么请升级投票/马克解决了;)我没有足够的声誉来升级投票:(,我可以核对你的答案。再次感谢:D@user3064132欢迎您!如果这对您有效,请向上投票/标记已解决;)我没有足够的声誉向上投票:(,我可以检查您的答案。再次感谢:D