Home  >  Article  >  Backend Development  >  PHP function explode() that splits a string and returns an array composed of strings

PHP function explode() that splits a string and returns an array composed of strings

PHP中文网
PHP中文网Original
2017-11-01 10:49:401857browse

Example

Break the string into an array:

<?php$str = "www.php.cn";print_r (explode(".",$str));?>

Definition and usage

explode() function uses one string to split another string and returns Array of strings.

Note: The "separator" parameter cannot be an empty string.

Note: This function is binary safe.

Syntax

explode(separator,string,limit)
<? 
// ### 切分字符串 #### 
function jb51netcut($start,$end,$file){ 
$content=explode($start,$file); 
$content=explode($end,$content[1]); 
return $content[0]; 
} 
?>

explode definition and usage
explode() function splits a string into an array.

This function returns an array composed of strings, each element of which is a substring separated by separator as a boundary point.
separator parameter cannot be an empty string. If separator is the empty string (""), explode() returns FALSE. If separator contains a value that is not found in string, explode() returns an array containing a single element from string.
If the limit parameter is set, the returned array contains at most limit elements, and the last element will contain the remainder of the string.
If the limit parameter is a negative number, all elements except the last -limit elements are returned. This feature is new in PHP 5.1.0.
In this example, we will split the string into an array:

<?php 
$str = "Hello world. It&#39;s a beautiful day."; 
print_r (explode(" ",$str)); 
?>

Output:

Array 
( 
[0] => Hello 
[1] => world. 
[2] => It&#39;s 
[3] => a 
[4] => beautiful 
[5] => day. 
)

Parameters                     Description

separator Required. Specifies where to split the string. ​

string Required. The string to split.

limit Optional. Specifies the number of array elements to be returned.

Possible values:

Greater than 0 - Returns an array containing at most limit elements

Less than 0 - Returns an array containing all but the last -limit elements Array

0 - will be treated as 1, returns an array containing one element

Technical detailsReturn value:

Return String array.

Use the limit parameter to return some array elements:

<?php
    $str = &#39;one,two,three,four&#39;; 
//  返回包含一个元素的数组
    print_r(explode(&#39;,&#39;,$str,0));print "<br>"; 
// 数组元素为 2
    print_r(explode(&#39;,&#39;,$str,2));print "<br>"; 
// 删除最后一个数组元素
    print_r(explode(&#39;,&#39;,$str,-1));
?>


The above is the detailed content of PHP function explode() that splits a string and returns an array composed of strings. 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