确保PHP substr完成一个单词而不是一个字符

我知道如何使用substr函数,但是这会很快结束string的一半。 我希望string在一个单词的末尾结束,我该如何去做这件事? 会涉及正则expression式吗? 任何帮助非常感谢。

这是我迄今为止。 只是SubStr …

echo substr("$body",0,260); 

干杯

这可以用一个正则expression式来完成,像这样的事情从string的开头到字边界将会达到260个字符:

 $line=$body; if (preg_match('/^.{1,260}\b/s', $body, $match)) { $line=$match[0]; } 

或者,也可以使用wordwrap函数将$ body分解成行,然后提取第一行。

 substr($body, 0, strpos($body, ' ', 260)) 

你可以试试这个:

  $s = substr($string, 0, 261); $result = substr($s, 0, strrpos($s, ' ')); 

你可以这样做:从第260个字符中find第一个空格,并将其用作裁剪标记:

 $pos = strpos($body, ' ', 260); if ($pos !== false) { echo substr($body, 0, $pos); } 

wordwrap和explode然后第一个数组元素是你想$wr=wordwrap($text,20,':'); $strs=explode(":",$wr); $strs[0] $wr=wordwrap($text,20,':'); $strs=explode(":",$wr); $strs[0]

 $pos = strpos($body, $wordfind); echo substr($body,0, (($pos)?$pos:260)); 

我使用这个解决scheme:

 $maxlength = 50; substr($name, 0, ($spos = strpos($name, ' ', $lcount = count($name) > $maxlength ? $lcount : $maxlength)) ? $spos : $lcount ); 

或内联:

 substr($name, 0, ($spos = strpos($name, ' ', $lcount = count($name) > 50 ? $lcount : 50)) ? $spos : $lcount ); 
 function substr_word($body,$maxlength){ if (strlen($body)<$maxlength) return $body; $body = substr($body, 0, $maxlength); $rpos = strrpos($body,' '); if ($rpos>0) $body = substr($body, 0, $rpos); return $body; } 

试试这个function

 <?php /** * trims text to a space then adds ellipses if desired * @param string $input text to trim * @param int $length in characters to trim to * @param bool $ellipses if ellipses (...) are to be added * @param bool $strip_html if html tags are to be stripped * @param bool $strip_style if css style are to be stripped * @return string */ function trim_text($input, $length, $ellipses = true, $strip_tag = true,$strip_style = true) { //strip tags, if desired if ($strip_tag) { $input = strip_tags($input); } //strip tags, if desired if ($strip_style) { $input = preg_replace('/(<[^>]+) style=".*?"/i', '$1',$input); } if($length=='full') { $trimmed_text=$input; } else { //no need to trim, already shorter than trim length if (strlen($input) <= $length) { return $input; } //find last space within length $last_space = strrpos(substr($input, 0, $length), ' '); $trimmed_text = substr($input, 0, $last_space); //add ellipses (...) if ($ellipses) { $trimmed_text .= '...'; } } return $trimmed_text; } ?> 
 public function Strip_text($data, $size, $lastString = ""){ $data = strip_tags($data); if(mb_strlen($data, 'utf-8') > $size){ $result = mb_substr($data,0,mb_strpos($data,' ',$size,'utf-8'),'utf-8'); if(mb_strlen($result, 'utf-8') <= 0){ $result = mb_substr($data,0,$size,'utf-8'); $result = mb_substr($result, 0, mb_strrpos($result, ' ','utf-8'),'utf-8');; } if(strlen($lastString) > 0) { $result .= $lastString; } }else{ $result = $data; } return $result; } 

将string传递给函数Strip_text(“使用html标签或不使用html标签的长文本”,15)然后,这个函数将返回htmlstring的前15个字符,而不使用html标签。 当string小于15个字符时,返回完整的string,否则返回15个字符$ lastString参数string。

例:

 Strip_text("<p>vijayDhanasekaran</p>", 5) 

结果:维杰

 Strip_text("<h1>vijayDhanasekaran<h1>",5,"***....") 

结果:vijay *** ….