php string function (2)

WBOY
Release: 2016-08-08 09:24:17
Original
946 people have browsed it

4. Format string function 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>
Copy after login
<code> 结果如下所示,number_format(str)表示在千分位上加,隔开。number_format(str,2)表示留两位小数点。number_format($str,2,",",".")把“,”替换成“.”。
</code>
Copy after login

5. Split the string into array function/emplode(). The function implode() that concatenates arrays into strings.

<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>
Copy after login

The result is as shown in the figure: explode(ch,str,limit). ch represents the delimiter that splits the string. str represents a string. Limit is optional, indicating how many times to split. The default is all.

<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>
Copy after login
<code>   结果如下图 。implode(separator,array)。 separator表示已什么来合成字符串。可选的。默认为空。array表示要合并成字符串的数组
</code>
Copy after login


6. Intercept string /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>
Copy after login
<code>  substr(str,separator)。str指的是所要截取的字符串。separtor表示从哪里开始截取。正数代表从左开始截取。负数代表从右开始截取。这里3个字符代表一个汉字。(utf-8里是三个字符一个汉字,gb2312里是二个字符一个汉字。)
</code>
Copy after login

The above introduces the PHP string function (2), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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