HTTP://localhost/website/?search=hello+world
这是网址,我只想从中提取 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
答案 0 :(得分: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
)
答案 1 :(得分:1)
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first; // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz
答案 2 :(得分:1)
我认为parse_ur()是你的朋友!
$url = 'HTTP://localhost/website/?search=hello+world';
$parsed = parse_url($url);
echo $parsed['query'] ; // search=hello+world
答案 3 :(得分:1)
您的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);