在Javascript我编码这样的请求参数的部件
window.location.href = "search.php?qry=" + encodeURIComponent("m & l");
在我的search.php我达到这个像这样
$url = urldecode($_SERVER['REQUEST_URI']);
echo ."Full URL: " .$url ."<br>";
$parts = parse_url($url);
parse_str($parts['query'], $query);
$qry = "Just Query: " .trim($query['qry']);
echo $qry ."<br>";
这种打印出:
Full Url: /Search.php?qry=m & l
Just Query: m
貌似&
后的东西是在并购l`被丢弃
我需要什么样的变化在PHP或JavaScript做什么呢?
只是改变:
$url = urldecode($_SERVER['REQUEST_URI']);
至
$url = $_SERVER['REQUEST_URI'];
你基本上是parse_url
将它以及解码双解码。
值得注意的是,PHP已经做到了这一点给你,让真正有没有一个理由来解析自己的URL。 $_GET['qry']
将包含'm & l'
如果你这样做了几个查询变量,你需要为每个单独运行encodeURIComponent
。
例:
window.location.href = "search.php?qry=" + encodeURIComponent("m & l") + "&subcat="+encodeURIComponent("hello & there");
你明确告诉它编码毕竟和。