Home > Backend Development > PHP Tutorial > 字符串长度问题

字符串长度问题

WBOY
Release: 2016-06-23 13:18:22
Original
880 people have browsed it

vardump打印一字符串
string(14) "00107042012"
获取到的字符串长度怎么是14
明明是11个数字啊!
为什么?


回复讨论(解决方案)

你的字符串有前有三个特殊字符(+BOM)

<?php$str = '00107042012';$str1 = '00107042012';var_dump($str);var_dump($str1);?>
Copy after login
Copy after login


看看两个输出就知道了。


那三个特殊字符的ascii码239,187,191就是BOM头的 EF BB BF
<?phpecho '<meta http-equiv="content-type" content="text/html; charset=utf-8">';$str = '00107042012';$str1 = '00107042012';echo 'str:<br>';for($i=0,$len=strlen($str);$i<$len;$i++){    echo ord(substr($str,$i,1)).'<br>';}echo '<br>str1:<br>';for($i=0,$len=strlen($str1);$i<$len;$i++){    echo ord(substr($str1,$i,1)).'<br>';}?>
Copy after login
Copy after login


str:2391871914848494855485250484950str1:4848494855485250484950
Copy after login
Copy after login


可以使用程序去掉。
<?php$str = '00107042012';$str = substr($str, 3);var_dump($str);?>
Copy after login
Copy after login

你的字符串有前有三个特殊字符(+BOM)

<?php$str = '00107042012';$str1 = '00107042012';var_dump($str);var_dump($str1);?>
Copy after login
Copy after login


看看两个输出就知道了。


那三个特殊字符的ascii码239,187,191就是BOM头的 EF BB BF
<?phpecho '<meta http-equiv="content-type" content="text/html; charset=utf-8">';$str = '00107042012';$str1 = '00107042012';echo 'str:<br>';for($i=0,$len=strlen($str);$i<$len;$i++){    echo ord(substr($str,$i,1)).'<br>';}echo '<br>str1:<br>';for($i=0,$len=strlen($str1);$i<$len;$i++){    echo ord(substr($str1,$i,1)).'<br>';}?>
Copy after login
Copy after login


str:2391871914848494855485250484950str1:4848494855485250484950
Copy after login
Copy after login


可以使用程序去掉。
<?php$str = '00107042012';$str = substr($str, 3);var_dump($str);?>
Copy after login
Copy after login



问题解决了,感谢版主的回复~下次知道了,转成ascii码输出就好了.么么哒
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template