Home >php教程 >php手册 >PHP截取UTF-8字符串,解决半字符问题

PHP截取UTF-8字符串,解决半字符问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-21 09:00:48841browse

function utf_substr($str,$len)
{
    for($i=0;$i    {
        $temp_str=substr($str,0,1);
        if(ord($temp_str) > 127){
            $i++;
        if($i            $new_str[]=substr($str,0,3);
            $str=substr($str,3);
            }
        }
    else {
        $new_str[]=substr($str,0,1);
        $str=substr($str,1);
        }
    }
    return join($new_str);
}



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