正如标题所说,我想在不破坏任何单词或 HTML 标签的情况下剪切一个字符串,现在我发现这个函数可以解决 html 标签问题(我自己稍微修改了)
function substrhtml($str,$start,$len){
$str_clean = substr(strip_tags($str),$start,$len);
if(preg_match_all('/\<[^>]+>/is',$str,$matches,PREG_OFFSET_CAPTURE)){
for($i=0;$i<count($matches[0]);$i++){
if($matches[0][$i][1] < $len){
$str_clean = substr($str_clean,0,$matches[0][$i][1]) . $matches[0][$i][0] . substr($str_clean,$matches[0][$i][1]);
}else if(preg_match('/\<[^>]+>$/is',$matches[0][$i][0])){
$str_clean = substr($str_clean,0,$matches[0][$i][1]) . $matches[0][$i][0] . substr($str_clean,$matches[0][$i][1]);
break;
}
}
return $str_clean;
}else{
return substr($str,$start,$len);
}
}
但它仍然在我不想发生的中间句子中减少了一半的单词,关于如何排序的任何想法?
一如既往地感谢任何帮助,并提前感谢。