首頁  >  文章  >  php教程  >  mb_系列函數和普通字元函數的區別

mb_系列函數和普通字元函數的區別

WBOY
WBOY原創
2016-12-05 13:26:281302瀏覽

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行,作用就很清楚了。

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn