Home  >  Article  >  Backend Development  >  Detailed introduction to PHP array to string_PHP tutorial

Detailed introduction to PHP array to string_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:27:19822browse

With the development of the times, our development technology has also been updated. PHP is still relatively commonly used, so I studied PHP array conversion to string, and I will share it with you here. I hope it will help Useful to everyone.

It is often necessary to use PHP to process strings. PHP's built-in character functions provide very powerful functions and can basically complete most character processing operations. For example, use the str_split function to convert a character into an array, the implode function to convert a PHP array into a string, the strpos function to find another character within a character, the substr function to obtain certain characters in a character, the strlen function to obtain the character length, etc. . These are the most basic knowledge of PHP, which is convenient for daily work. I have made some summary of this function for reference only.

1, how to convert a character into an array
Solution: str_split() function
The example is as follows:

<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>

Output result:

<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. How to convert a php array to a string
Solution: implode() function
The example is as follows:

<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>

Output result biiuu

3, how to find another character
in one character Solution: strpos or strstr
Example is as follows:

<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>

Output result: 19

4, how to get certain characters in the characters
Solution: substr() function
Example is as follows:

<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>

Output result: biuuu.com

5, how to get the character length
Solution: strlen() function
The example is as follows:

<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>

Output result 28

The above PHP character processing functions str_split, implode, strpos, substr and strlen are often used in actual development. I hope they will be helpful to everyone.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446527.htmlTechArticleWith the development of the times, our development technology has also been updated, and PHP is still relatively commonly used, so I I have studied PHP array conversion to string, and I will share it with you here...
Statement:
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