js与php中一些相似函数的对比

一: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中截取中英文混用函数,还是找通用的截取函数,不要用内部的这两个函数

 
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值