Home  >  Article  >  php教程  >  php字符串截取函数(支持中文 utf-8截取)

php字符串截取函数(支持中文 utf-8截取)

WBOY
WBOYOriginal
2016-06-13 11:18:032166browse

 

php教程字符串截取函数(支持中文 utf-8截取)
本文章提供的这二款汉字截取函数方法主要是针对utf8编码如何正确截取汉字的这个问题来做的,

http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">





$str = "中12hb千钧一发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"); //共有多少字
    //$elen = 8; //每行字符串长度8个字符、4个汉字
    $dlen = 0; //每行显示长度
    $str_wrap = '';
    for($i=0;$i     {
        $tmpchar = mb_substr($str,$i,1,"utf8");
        if(strlen($tmpchar) == 3)
            $charlen = 2;
        else
            $charlen = 1;
        if( $dlen         {
            $dlen += $charlen;
            $str_wrap .= $tmpchar;
        }
        else
        {
            $str_wrap .= "
".$tmpchar;
            $dlen = $charlen;
        }
    }
    return $str_wrap;
}

echo str_wrap1($str,5);

?>


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