? PHP ?,urlencode()
??????????? URL ?????????,????????????? URL ?????????
?? PHP ?????? urlencode()
,???????:
- ??,???????????????????,???????????????????
$input = "This is a sample string with special characters: !*'();:@&=+$,/?%#[]";
- ??
urlencode()
???????????
$encoded_string = urlencode($input);
- ??,
$encoded_string
?????? URL ?????,??????? URL ??
$url = "https://example.com/search?query=" . $encoded_string;
- ????????????????????,????
urldecode()
???
$decoded_string = urldecode($encoded_string);
??:urlencode()
??????????????????? URL(?????????),????? parse_url()
? http_build_query()
?????? URL ?????,????????
??:
$url = 'https://example.com/???key=value&test=??'; // ?? URL $parsed_url = parse_url($url); // ??????? $encoded_path = array_map('urlencode', explode('/', $parsed_url['path'])); $encoded_path = implode('/', $encoded_path); // ????????? $query_params = []; parse_str($parsed_url['query'], $query_params); $encoded_query = http_build_query($query_params); // ???? URL $encoded_url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . '/' . $encoded_path . '?' . $encoded_query; echo $encoded_url;
????:
https://example.com/测试?key=value&test=测试