HTTP://localhost/website/?search=hello+world
这是URL,我只想从中提取hello world。
现在我正在使用以下代码:
if(isset($_GET['q'])) {
$url = $_GET['q'];
$x = rtrim($url,'/');
$x = explode('/',$url);
print_r($x);
echo($url);
}
但这在问号之后没有显示任何内容。此代码或.htaccess
文件是否有任何问题。
我还包含带有代码的.htaccess
文件:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-+/]+)$ index.php?q=$1
RewriteRule ^([a-zA-Z0-9-+/]+)/$ index.php?q=$1
使用 php 内置函数 parse_url()
http://php.net/manual/en/function.parse-url.php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
结果
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
我认为parse_ur()是你的朋友!
$url = 'HTTP://localhost/website/?search=hello+world';
$parsed = parse_url($url);
echo $parsed['query'] ; // search=hello+world
你的htaccess
规则是错误的。
它必须是
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-+/]+)$ index.php?search=$1
RewriteRule ^([a-zA-Z0-9-+/]+)/$ index.php?search=$1
并且代码应该是
if(isset($_GET['search'])) {
$url = $_GET['search'];
$x = rtrim($url,'/');
$x = explode('/',$url);
print_r($x);
echo($url);
您可以使用
parse_url和parse_str函数:
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first; // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz