随着时代的发展,我们的开发技术也随着更新,对于PHP还是比较常用的,于是我研究了一下PHP数组转字符串,在这里拿出来和大家分享一下,希望对大家有用。
1,如何将一个字符转换成一个数组
解决方法:str_split()函数
示例如下:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">biuuu</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">'www.biuuu.com'</font></span><span>; </span></span></li> <li class=""><span>print_r(str_split($biuuu)) </span></li> </ol>
输出结果:
<ol class="dp-xml"> <li class="alt"><span><span>Array </span></span></li> <li class=""><span>( </span></li> <li class="alt"> <span>[0] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> b </span> </li> <li class=""> <span>[1] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> i </span> </li> <li class="alt"> <span>[2] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> u </span> </li> <li class=""> <span>[3] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> u </span> </li> <li class="alt"> <span>[4] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> u </span> </li> <li class=""><span>) </span></li> </ol>
2,如何将一个php数组转字符串
解决方法:implode()函数
示例如下:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">biuuu</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>('b','i','u','u','u'); </span></span></li> <li class=""><span>print_r(implode('',$biuuu)); </span></li> </ol>
输出结果biiuu
3,如何在一个字符中查找另一个字符
解决方法:strpos或strstr
示例如下:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">biuuu</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">'this is my website biuuu.com'</font></span><span>; </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">search</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">'biuuu'</font></span><span>; </span> </li> <li class="alt"><span>print_r(strpos($biuuu,$search)); </span></li> </ol>
输出结果:19
4,如何获取字符中的某几个字符
解决方法:substr()函数
示例如下:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">biuuu</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">'this is my website biuuu.com'</font></span><span>; </span></span></li> <li class=""><span>print_r(substr($biuuu,19)); </span></li> </ol>
输出结果:biuuu.com
5,如何获取字符长度
解决方法:strlen()函数
示例如下:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">biuuu</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">'this is my website biuuu.com'</font></span><span>; </span></span></li> <li class=""><span>print_r(strlen($biuuu)); </span></li> </ol>
输出结果28
以上几个PHP字符处理函数str_split、implode、strpos、substr和strlen在实际开发中由于经常需要使用,希望对大家有帮助。