php explode function instance code

  • 2020-05-12 02:23:14
  • OfStack

The explode() function splits a string into an array.
grammar
explode(separator,string,limit)
parameter describe separator A necessity. Specifies where to split the string. string A necessity. The string to be split. limit Optional. Specifies the maximum number of array elements to return.
instructions
This function returns an array of strings, each element of which is a substring separated by separator as a boundary point.

The separator parameter cannot be an empty string. If separator is an empty string (""), explode() returns FALSE. If the value contained in separator is not found in string, explode() returns an array containing the individual elements in string.

If the limit parameter is set, the returned array contains up to limit elements, and the last element will contain the rest of string.

If the limit parameter is negative, all elements except the last -limit element are returned. This feature is new to PHP 5.1.0.
Hints and comments
Note: the parameter limit was added in PHP 4.0.1.

Note: for historical reasons, implode() can accept both parameter orders, but explode() cannot. You must make sure that the separator parameter precedes the string parameter.
example
In this example, we will split the string into an array:
 
<?php 
$str = "Hello world. It's a beautiful day."; 
print_r (explode(" ",$str)); 
?> 

Output:

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

explode function example tutorial
explode ( string separator, string string [, int limit] )
separator is an empty string (""), explode() returns FALSE.
If the value contained by separator is not found in string, explode() returns an array containing a single element of string.
 
//explode  The instance 1 
$explode = "aaa,bbb,ccc,ddd,explode,jjjj"; 
$array = explode( ',' ,$explode ); 
print_r($array); 
/* 
 The results for  
Array 
( 
[0] => aaa 
[1] => bbb 
[2] => ccc 
[3] => ddd 
[4] => explode 
[5] => jjjj 
) 
*/ 

// we can use the explode and end functions when dealing with dates or getting file extensions. Let's take a look at an example
 
$file ="www.ofstack.com.gif"; 
$extArray = explode( '.' ,$file ); 
$ext = end($extArray); 
echo $ext; 
/* 
 The output value of .gif 

There are errors in using these functions
Note: Separator cannot be an empty string. Note: the separator cannot be an empty string.
The string to be split is empty

Definition and Usage does not use the segmentation function
It may be that the segmentation character you set does not exist

Related articles: