Home  >  Article  >  Backend Development  >  What is the function to split string in php

What is the function to split string in php

(*-*)浩
(*-*)浩Original
2019-10-08 14:51:186849browse

php functions to split strings include explode() and str_split()

What is the function to split string in php

##explode()

This function is the inverse function of implode(). It uses one string to split another string and returns an array.

Grammar: (Recommended learning: PHP video tutorial)

array explode( string separator, string string [, int limit] )<br/>

Example:

<?php<br/>$str = &#39;one|two|three|four&#39;;<br/>print_r(explode(&#39;|&#39;, $str));<br/>print_r(explode(&#39;|&#39;, $str, 2));<br/>// 负数的 limit(自 PHP 5.1 起)<br/>print_r(explode(&#39;|&#39;, $str, -1));<br/>?><br/>

The output results are as follows:

Array<br/>(<br/>    [0] => one<br/>    [1] => two<br/>    [2] => three<br/>    [3] => four<br/>)<br/>Array<br/>(<br/>    [0] => one<br/>    [1] => two|three|four<br/>)<br/>Array<br/>(<br/>    [0] => one<br/>    [1] => two<br/>    [2] => three<br/>)<br/>

str_split()

str_split() splits the string into an array and returns an array successfully.

Syntax:

array str_split( string string [, int length] )<br/>

Example:

<?php<br/>$str = &#39;one two three&#39;;<br/>$arr1 = str_split($str);<br/>$arr2 = str_split($str, 3);<br/>print_r($arr1);<br/>print_r($arr2);<br/>?><br/>

The output is as follows:

Array<br/>(<br/>    [0] => o<br/>    [1] => n<br/>    [2] => e<br/>    [3] =>  <br/>    [4] => t<br/>    [5] => w<br/>    [6] => o<br/>    [7] =>  <br/>    [8] => t<br/>    [9] => h<br/>    [10] => r<br/>    [11] => e<br/>    [12] => e<br/>)<br/>Array<br/>(<br/>    [0] => one<br/>    [1] =>  tw<br/>    [2] => o t<br/>    [3] => hre<br/>    [4] => e<br/>)<br/>

The above is the detailed content of What is the function to split string in php. For more information, please follow other related articles on the PHP Chinese website!

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