Rumah > Artikel > pembangunan bahagian belakang > php字符串函数(2)
四.格式化字符串函数 number_format()
<code><span>$str</span> = <span>"123445677"</span>; <span>$strl</span> = number_format(<span>$str</span>); <span>$str2</span> = number_format(<span>$str</span>,<span>2</span>); <span>$str3</span> = number_format(<span>$str</span>,<span>2</span>,<span>","</span>,<span>'.'</span>); <span>echo</span><span>$str</span>.<span>"<br>"</span>; <span>echo</span><span>$strl</span>.<span>"<br>"</span>; <span>echo</span><span>$str2</span>.<span>"<br>"</span>; <span>echo</span><span>$str3</span>.<span>"<br>"</span>;</code>
<code> 结果如下所示,number_format(str)表示在千分位上加,隔开。number_format(str,2)表示留两位小数点。number_format($str,2,",",".")把“,”替换成“.”。 </code>
五.把字符串分割成数组函数/emplode()。把数组连接成字符串函数implode()。
<code><span>$str</span> = <span>"你好@php编程@每天进步一点"</span>; <span>$tem</span> = explode(<span>'@'</span>,<span>$str</span>); <span>$teml</span> = explode(<span>'@'</span>,<span>$str</span>,<span>2</span>); print_r(<span>$tem</span>); <span>echo</span><span>"<br>"</span>; print_r(<span>$teml</span>);</code>
结果如图:explode(ch,str,limit)。ch表示分割字符串的分割符。str表示字符串。limit可选,表示要分割几次,默认为全部。
<code><span>$array</span> = <span>array</span>(<span>"hello"</span>,<span>"php编程"</span>,<span>"每天进步一点"</span>); <span>$str</span> = implode(<span>","</span>,<span>$array</span>); print_r(<span>$str</span>);</code>
<code> 结果如下图 。implode(separator,array)。 separator表示已什么来合成字符串。可选的。默认为空。array表示要合并成字符串的数组 </code>
六.截取字符串 /substr(str,separator)
<code><span>$str</span> = <span>"hello,php编程"</span>; <span>$strm</span> = substr(<span>$str</span>,<span>3</span>); <span>$strl</span> = substr(<span>$str</span>,-<span>3</span>); <span>echo</span><span>$strm</span>.<span>"<br>"</span>; <span>echo</span><span>$strl</span>;</code>
<code> substr(str,separator)。str指的是所要截取的字符串。separtor表示从哪里开始截取。正数代表从左开始截取。负数代表从右开始截取。这里3个字符代表一个汉字。(utf-8里是三个字符一个汉字,gb2312里是二个字符一个汉字。) </code>
以上就介绍了php字符串函数(2),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。