php deletes the implementation code that first appears a character in a string

  • 2020-05-27 04:36:29
  • OfStack

$a = " string ";
$c= explode(" The text to delete ", $a, 2); 
$b = $c[0].$c[1]; 

(PHP 3, PHP 4, PHP 5)

explode -- use one string to split another string
array explode ( string separator, string string [, int limit] )

This function returns an array of strings, each of which is a substring of string, separated by the string separator as a boundary point. 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 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.
If the limit parameter is negative, all elements except the last limit element are returned. This feature is new to PHP 5.1.0.
For historical reasons, while implode() can accept both parameter orders, explode() cannot. You must make sure that the separator parameter precedes the string parameter.

Note: the parameter limit was added in PHP 4.0.1.
Example 1. explode() example
//  The sample  1 
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; 
$pieces = explode(" ", $pizza); 
echo $pieces[0]; // piece1 
echo $pieces[1]; // piece2 

//  The sample  2 
$data = "foo:*:1023:1000::/home/foo:/bin/sh"; 
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data); 
echo $user; // foo 
echo $pass; // * 

Example 2. Example of the limit parameter
$str = 'one|two|three|four'; 
//  The positive  limit 
print_r(explode('|', $str, 2)); 
//  negative  limit 
print_r(explode('|', $str, -1)); 

The above example will output:

[0] = > one
[1] = > two|three|four
[0] = > one
[1] = > two
[2] = > three

Note: this function is safe for binary objects.

Related articles: