composer @Ta | |
2018-09-13 00:08 |
/**
* 字符串切割+过滤+转换
*
* 功能:截取字符串(支持中文)
* 如果截取的字符串中不包含html标签,则正常截取
* 如果字符串中包括img标签,则先进行过去标签,截取后,将标签位置放回,截取的字符串则会保留完整的html标签
*
* @param string $string
* @param unknown $length
* @param string $replace
* @return string
*/
function cutTab($string, $length='15', $dot = '…') {
$_lenth = mb_strlen($string, "utf-8");
$text_str = preg_replace("/<img.*?>/si","",$string);
$text_lenth = mb_strlen($text_str, "utf-8") - 1;
if($text_lenth <= $length) {
return stripcslashes($string);
}
if(strpos($string, '<img') === false){
$res = mb_substr($string, 0, $length, 'UTF-8');
return stripcslashes($res).$dot;
}
//计算标签位置
$html_start = ceil(strpos($string, '<img') / 3);
$html_end = ceil(strpos($string, '/>') / 3);
if($length < $html_start) {
$res = mb_substr($string, 0, $length, 'UTF-8');
return stripcslashes($res).$dot;
}
if($length > $html_start) {
$res_html = mb_substr($text_str, 0, $length-1, 'UTF-8');
preg_match('/<img[^>]*>/',$string,$result_html);
$before = mb_substr($res_html, 0, $html_start, 'UTF-8');
$after = mb_substr($res_html, $html_start, mb_strlen($res_html, "utf-8"), 'UTF-8');
$res = $before.$result_html[0].$after;
return stripcslashes($res).$dot;
}
}
阅:2936 | 发表于2018-09-13 00:08 |
心灵鸡汤:
      
®2019-2024 all power by Zlproject3.7
川公网安备 51192202000151号
蜀ICP备2021014921号-2
首页 #top
天合之作 曲在人意