首页 > php教程 > php手册 > mb_系列函数和普通字符函数的区别

mb_系列函数和普通字符函数的区别

WBOY
发布: 2016-12-05 13:26:28
原创
1325 人浏览过

mb函数是根据编码格式 处理文字的函数扩展

修改/etc/php.ini配置文件,增加extension=php_mbstring.so

 

 用我最常用的substr函数举例子。一目了然。

举例:

<span style="color: #008080"> 1</span> <span style="color: #000000">php
</span><span style="color: #008080"> 2</span>         <span style="color: #008000">//</span><span style="color: #008000">phpinfo();</span>
<span style="color: #008080"> 3</span>         <span style="color: #800080">$str</span> = 'abcdef'<span style="color: #000000">;
</span><span style="color: #008080"> 4</span>         <span style="color: #0000ff">echo</span> <span style="color: #008080">strlen</span>(<span style="color: #800080">$str</span>);<span style="color: #008000">//</span><span style="color: #008000"> 6</span>
<span style="color: #008080"> 5</span>         <span style="color: #0000ff">echo</span> '<br>'<span style="color: #000000">;
</span><span style="color: #008080"> 6</span>         <span style="color: #0000ff">echo</span> <span style="color: #008080">substr</span>(<span style="color: #800080">$str</span>, 1,2);<span style="color: #008000">//</span><span style="color: #008000"> bc</span>
<span style="color: #008080"> 7</span>         <span style="color: #0000ff">echo</span> '<br>'<span style="color: #000000">;
</span><span style="color: #008080"> 8</span> 
<span style="color: #008080"> 9</span>         <span style="color: #800080">$str2</span> = '我是谁ab'<span style="color: #000000">;
</span><span style="color: #008080">10</span>         <span style="color: #0000ff">echo</span> <span style="color: #008080">substr</span>(<span style="color: #800080">$str2</span>, 2, 2);<span style="color: #008000">//</span><span style="color: #008000"> ��</span>
<span style="color: #008080">11</span>         <span style="color: #0000ff">echo</span> '<br>'<span style="color: #000000">;
</span><span style="color: #008080">12</span>         <span style="color: #0000ff">echo</span> mb_substr(<span style="color: #800080">$str2</span>, 2, 2, 'UTF-8');<span style="color: #008000">//</span><span style="color: #008000"> 谁a</span>
<span style="color: #008080">13</span>         <span style="color: #0000ff">echo</span> '<br>'<span style="color: #000000">;
</span><span style="color: #008080">14</span>         <span style="color: #0000ff">echo</span> <span style="color: #008080">strlen</span>(<span style="color: #800080">$str2</span>);<span style="color: #008000">//</span><span style="color: #008000"> 11</span>
<span style="color: #008080">15</span>         <span style="color: #0000ff">echo</span> '<br>'<span style="color: #000000">;
</span><span style="color: #008080">16</span>         <span style="color: #0000ff">echo</span> mb_strlen(<span style="color: #800080">$str2</span>, 'UTF-8');<span style="color: #008000">//</span><span style="color: #008000"> 5</span>
<span style="color: #008080">17</span>         <span style="color: #0000ff">echo</span> '<br>';
登录后复制

 

看第10和12行,作用就很清楚了。

 

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板