Home >Backend Development >PHP Tutorial >PHP string interception function (supports Chinese utf-8 interception)_PHP tutorial

PHP string interception function (supports Chinese utf-8 interception)_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:01:521039browse

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">





$str = "12hb critical lap";
echo mb_substr($str,0,5,'utf-8');echo '
';
echo mb_substr($str,5,3,'utf-8');echo '
';
echo mb_substr($str,8,4,'utf-8');
?>


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);

?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445415.htmlTechArticlephp tutorial string interception function (supports Chinese utf-8 interception) The two Chinese character interception functions provided in this article The method is mainly aimed at the problem of how to correctly intercept Chinese characters using utf8 encoding...
Statement:
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