Home > Backend Development > PHP Tutorial > Discuss the specific ways of splitting strings in PHP_PHP Tutorial

Discuss the specific ways of splitting strings in PHP_PHP Tutorial

WBOY
Release: 2016-07-15 13:31:04
Original
979 people have browsed it

We are using Result: Array ( [0] => 1 [1] => 2 [2] => ; 3 [3] => 4 )

Definition and usage
str_split() function splits a string into an array.

Syntax length represents the number of each string

str_split(string,length) The default length is 1. If the above is str_split($str,2) the result is Array ([0] = > 12 [1] => 34)

If $str='1 2 3 4' then use the explode() function explode(' ',$str) to separate by spaces

If it is the content of an article, the interception method is based on the number of characters: use the strlen() function and substr() string interception function to implement the function of PHP splitting strings, such as:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?php $</span><span class="attribute">len</span><span>=</span><span class="attribute-value">strlen</span><span>($row['game_detail']);  </span></span></li><li><span>//获取这个字符串的长度;   </span></li><li class="alt"><span>if($len</span><span class="tag">></span><span>102)   </span></span></li>
<li><span>//判断字符串如果大于102就输出102个字符串。   </span></li>
<li class="alt">
<span>{ echo $</span><span class="attribute">s</span><span>=</span><span class="attribute-value">substr</span><span>($row['game_detail'],<br>0,102); echo '...';   </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>else{ echo $row["game_detail"];   </span></li>
<li><span>//否则就输出全部;   </span></li>
<li class="alt"><span>}   </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

That is, make a judgment before outputting. . strlen (string name). . substr(String, 0, 102) The range to split 0-103. The above are all the methods of splitting strings in PHP. I hope you can master them.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446237.htmlTechArticleWe are using the result: Array ( [0] = 1 [1] = 2 [2] = 3 [3 ] = 4) Definition and usage The str_split() function splits a string into an array. Syntax length represents the number of each string...
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