繁体   English   中英

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

[英]php - how to extract a particular word from url removing question mark

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_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 

我认为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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM