How can I reuse the escaped value of a query string in PHP?
I'd like to reuse a multiple-term search query with +
for escaping spaces.
$_SERVER['QUERY_STRING'] results in
q=my+search+term
while $_GET['q'] 开发者_StackOverflow中文版results in
my search term
How can I get
my+search+term
does it have to be regex?
urlencode($_GET['q']);
$q = str_replace(' ', '+', $_GET['q'])
or
$q = urlencode($_GET['q']);
No, you can use str_replace
:
$str = str_replace(' ', '+', $str);
but if there are more special characters, urlencode
is the better choice:
$str = urlencode($str);
$clean = urldecode($_SERVER['QUERY_STRING']);
http://www.php.net/manual/fr/function.urldecode.php
精彩评论