一:substr

js中:stringObject.substr(start,length)   一个中文算一个字符,一个英文也算一个字符  

<script type="text/javascript">
var str="我们ello world!"      
document.write(str.substr(1,2))  //输出“们e”
</script>

php中:

string substr ( string $string , int $start [, int $length ] )   这个函数对英文截取有效,对中文结果很变态,乱码
string mb_substr ( string $str , int $start [, int $length [, string $encoding ]] )  当$encoding为utf-8时,一个中文一个字符,一个英文一个字符
$encoding为gbk时,截取会出现乱码
header ('Content-type: text/html; charset=utf-8');
echo substr('我们ello world!', 1, 3); //输出 “��”
echo substr('我们ello world!', 2, 3); //输出 “���”

echo mb_substr('我们ello world!', 1, 3, 'utf-8'); //输出“们el” echo mb_substr('我们ello world!', 2, 3, 'utf-8'); //输出“ell”

echo mb_substr('我们ello world!', 1, 3, 'gb2312');  //输出“浠�”   
echo mb_substr('我们ello world!', 2, 3, 'gb2312');  //输出“ell
exit;

综上所述:php中截取中英文混用函数,还是找通用的截取函数,不要用内部的这两个函数

 
 

 

相关文章:

  • 2021-11-14
  • 2022-03-08
  • 2022-12-23
  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
  • 2021-10-26
  • 2022-02-20
猜你喜欢
  • 2021-12-07
  • 2022-01-03
  • 2022-12-23
  • 2022-03-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案