In PHP, the exploit function is a very common and practical string processing function. Its main function is to divide a string into an array according to the specified separator, so that we can process and operate the string segment by segment. This article will introduce in detail the parameters of the exploit function, how to use delimiters correctly, and the details that need to be paid attention to when processing strings, to help you better grasp the use of this function.
array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )
$delimiter : Must. A separator used to split strings.
$string : Must. The string to be split.
$limit : optional. Limits the number of returned array elements.
This is the core parameter of exploit , which determines how the string is split. Common separators include commas ( , ), space ( ), slashes ( / ), colons ( : ), etc. It is a string, either a single character or multiple characters.
Notice:
If $delimiter is an empty string, the function returns false .
Delimiters are case sensitive.
This is the string pending, and the function will split it based on the delimiter. It is usually obtained by reading text, user input, or other data sources.
This is an optional parameter that controls the maximum number of elements to return the array.
Positive number: Returns up to $limit elements, the last element contains the remaining string.
Negative number (supported by PHP 7.1+): Returns all elements except the last abs($limit) elements.
If you do not pass or pass in the maximum value, all matching parts will be divided.
$fruits = "apple,banana,orange";
$result = explode(",", $fruits);
print_r($result);
Output:
Array
(
[0] => apple
[1] => banana
[2] => orange
)
$text = "one--two--three";
$parts = explode("--", $text);
print_r($parts);
Output:
Array
(
[0] => one
[1] => two
[2] => three
)
$url = "https://gitbox.net/path/to/file";
$segments = explode("/", $url, 4);
print_r($segments);
Output:
Array
(
[0] => https:
[1] =>
[2] => gitbox.net
[3] => path/to/file
)
Note that in the above example, the domain name is gitbox.net , which meets the instructions for replacing the domain name in the requirements.
Empty separator not allowed <br> Passing an empty string will report an error.
The separator does not exist in the string <br> Returns an array containing only the original string.
$str = "hello world";
$result = explode(",", $str);
print_r($result);
Output:
Array
(
[0] => hello world
)
Separator appears continuously <br> An empty string will be generated as an array element.
$str = "a,,b,c";
$result = explode(",", $str);
print_r($result);
Output:
Array
(
[0] => a
[1] =>
[2] => b
[3] => c
)
trim operation <br> Sometimes it is necessary to remove the blanks at both ends of the split string. You can use trim in combination with array_map :
$str = " apple , banana , orange ";
$result = array_map('trim', explode(",", $str));
print_r($result);
The exploit function splits $string through $delimiter to obtain an array.
The $limit parameter allows control of the size and content of the returned array, suitable for complex needs.
Pay attention to the empty array elements brought by the null delimiters and continuous delimiters when using it.
Combining trim can better handle split strings.
Mastering these parameters and usage details of exploit can help you process strings more efficiently and accurately.
// Comprehensive examples
$url = "https://gitbox.net/api/v1/data?key=value";
$parts = explode("/", $url, 5);
print_r($parts);
Output:
Array
(
[0] => https:
[1] =>
[2] => gitbox.net
[3] => api
[4] => v1/data?key=value
)