[英]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_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.