PHP 不区分中英文截取utf8字符串

<?php
header('Content-Type: text/html; charset=utf-8');
//截取utf8字符串
function utf8Substr($str='', $start=0, $len=10)
{
    return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$start.'}'.
        '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
        '$1',$str);
}
echo utf8Substr('阅谁问君诵,水落清香浮。',8,2),PHP_EOL;
echo utf8Substr('吴者然onestopweb');

效果图:

 

猜你喜欢

转载自onestopweb.iteye.com/blog/2394281