composer @Ta | |
2018-09-13 00:04 |
/**
* 字符串切割
* 功能:截取字符串(支持中文),如果字符串中包括html标签,截取的字符串则会保留完整的html标签
* @param string $string 输入字符串
* @param unknown $length 截取长度
* @param bool $mode 截取模式:0删去html标记(默认),1补齐html标记
* @param string $replace
* @return string
* @author 未来往事<http://blog.fity.cn>
* 说明:
* 1.未考虑多字节字符,仅已字节做计数单位
* 2.未考虑可单独存在的标记
*/
function html_substr($string, $length, $mode=0, $omit = '......') {
//先截取指定长度的字符串开始--临时添加
if (strlen ( $string ) < $length) {
// $string = substr ( $string, 0 );
$string = mb_substr($string,0,'',"utf-8");
} else {
// $string = substr ( $string, 0, $length );
$string = mb_substr($string,0,$length,"utf-8");
}
//先截取指定长度的字符串结束
// var_dump($string);die;
$str= preg_split('/(<!--.*-->|<[^>]*>)/s', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach($str AS $k => $v) {
if($v{0} != '<') {
$length = $length - strlen($v);
if($length < 0) $str[$k] = mb_substr($v, 0, $length, "utf-8");
}else{
$str[$k] = strtolower($v);
}
if($length <= 0){
break;
}
}
//var_dump($str);die;
$str = array_slice($str, 0, $k+1);
$length = count($str);
foreach($str as $k=>$v) {
if($v{0} == '<' && $v[1] != '/') {
$ch = str_replace('<', '</', $v);
for($i=$k+1; $i<$length && $str[$i]!=$ch; $i++);
if($i == $length){
if($mode){
if($str[$length] == '</br/>'){
$str[$length] = '';
}
if($ch == '</br/>'){
$ch = '';
}
$str[$length] = $ch . $str[$length];
}else{
$str[$k] = '';
}
}
}
}
//var_dump($str);die;
$str = join('', $str).$omit;
return $str;
}
阅:2504 | 发表于2018-09-13 00:04 |
心灵鸡汤:
      
®2019-2024 all power by Zlproject3.7
川公网安备 51192202000151号
蜀ICP备2021014921号-2
首页 #top
天合之作 曲在人意