php tutorial string interception function (supports Chinese utf-8 interception)
The two Chinese character interception function methods provided in this article are mainly aimed at the problem of how to correctly intercept Chinese characters in utf8 encoding,
http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
function str_wrap1($str,$elen=30)
{
$tlen = mb_strlen($str,"utf8"); //How many words are there
//$elen = 8; //The length of each line of string is 8 characters and 4 Chinese characters
$dlen = 0; //Display length of each line
$str_wrap = '';
for($i=0;$i<$tlen;$i++)
{
$tmpchar = mb_substr($str,$i,1,"utf8");
If(strlen($tmpchar) == 3)
$charlen = 2;
else
$charlen = 1;
if( $dlen < $elen-1 )
{
$dlen += $charlen;
$str_wrap .= $tmpchar;
}
else
{
$str_wrap .= "
".$tmpchar;
$dlen = $charlen;
}
}
Return $str_wrap;
}echo str_wrap1($str,5);
?>