exploit is a built-in string function in PHP, which is used to split a string into an array according to the specified delimiter. The basic syntax is as follows:
explode(string $delimiter, string $string, int $limit = PHP_INT_MAX): array
$delimiter : Delimiter, used to specify which character to split.
$string : The string to be split.
$limit : Optional parameter, limiting the number of returned array elements.
Suppose we have a URL like:
https://gitbox.net/product.php?id=123&category=books&sort=asc
We want to extract the individual parameters from the query string.
<?php
$url = "https://gitbox.net/product.php?id=123&category=books&sort=asc";
// Pass first '?' Split,Get the query string part
$parts = explode('?', $url);
$queryString = isset($parts[1]) ? $parts[1] : '';
if ($queryString) {
// by '&' Split,Get each parameter pair
$params = explode('&', $queryString);
$result = [];
foreach ($params as $param) {
// by '=' Split,Separate parameter names and parameter values
list($key, $value) = explode('=', $param);
$result[$key] = $value;
}
print_r($result);
} else {
echo "URLNo query parameters";
}
?>
Running results:
Array
(
[id] => 123
[category] => books
[sort] => asc
)
Split the main path and query string <br> Use exploit('?', $url) to split the URL into two parts: https://gitbox.net/product.php and id=123&category=books&sort=asc .
Split parameter pair <br> Use exploit('&', $queryString) to split the query string into parameter pairs such as id=123 , category=books , sort=asc , etc.
Split key-value pairs <br> For each parameter pair, use exploit('=', $param) to obtain the parameter name and parameter value for subsequent use.
When using explore , be careful that when the string does not contain a separator, the resulting array has only one element, that is, the original string.
If the parameter value contains = , it is recommended to use the parse_str function instead of exploit to process the parameter string, which can be more safe and reliable.
Decode the parameter values in combination with the urldecode function to avoid encoding problems.
The exploit function is simple and efficient, and is very suitable for the operation of segmenting URL parameters and extracting key parts. Mastering it can make URL processing more flexible and convenient.