Home > Backend Development > PHP Tutorial > substr()函数中文版_PHP

substr()函数中文版_PHP

WBOY
Release: 2016-06-01 12:38:35
Original
889 people have browsed it

SUBSTR


substr()函数中文版 普通的substr()函数可以取得字符串的指定长度子字符串,但遇到中文时可能会在新字符串末尾产生乱码,下面这个函数将超过$len长度的字符串转换成以“...”结尾,并且去除了乱码。

用法:$new = getsubstring($old,20);

function getsubstring($str,$len)
{
    for($i = 0;$i     {
        if ($i >=0 AND $i         {
            if(ord(substr($str,$i,1)) > 0xa1)  
                $result_str.=substr($str,$i,2);
            else
                $result_str.=substr($str,$i,1);
        }
        if(ord(substr($str,$i,1)) > 0xa1)
            $i++;
    }
    if(strlen($str)        return $result_str;
    else
        return $result_str."...";
}
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