Home > Backend Development > PHP Tutorial > PHP代码utf-8中文截取函数,单字节截取模式

PHP代码utf-8中文截取函数,单字节截取模式

WBOY
Release: 2016-06-20 13:01:57
Original
1069 people have browsed it

PHP代码utf-8中文截取函数,单字节截取模式

/**
*  utf-8中文截取,单字节截取模式
*
* @access public
* @param string $str 需要截取的字符串
* @param int $slen 截取的长度
* @param int $startdd 开始标记处
* @return string
*/
function cn_substr_utf8($str, $length, $start=0) {
if(strlen($str) < $start+1) {
return '';
}
preg_match_all("/./su", $str, $ar);
$str = '';
$tstr = '';
//为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取
for($i=0; isset($ar[0][$i]); $i++) {
if(strlen($tstr) < $start) {
$tstr .= $ar[0][$i];
} else {
if(strlen($str) < $length + strlen($ar[0][$i]) ) {
$str .= $ar[0][$i];
} else {
break;
}
}
}
return $str;
}
Copy after login

 


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template