php - 如何从网址中删除问号提取特定单词

时间:2013-09-18 08:11:07

标签: php .htaccess

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

4 个答案:

答案 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)

您可以使用parse_urlparse_str功能:

$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);