HTTP://localhost/website/?search=hello+world
This is the URL and I just want to extract hello world from it
Right now i am using this code:
if(isset($_GET['q'])) {
$url = $_GET['q'];
$x = rtrim($url,'/');
$x = explode('/',$url);
print_r($x);
echo($url);
}
But this does not show anything after the question mark. Is there anything wrong with this code or the .htaccess
file.
I have also included the .htaccess
file with code:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-+/]+)$ index.php?q=$1
RewriteRule ^([a-zA-Z0-9-+/]+)/$ index.php?q=$1
Use php inbuilt function 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));
results
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
You can use parse_url and parse_str function:
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first; // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz
I think parse_ur() is your friend!
$url = 'HTTP://localhost/website/?search=hello+world';
$parsed = parse_url($url);
echo $parsed['query'] ; // search=hello+world
Your htaccess
rule is wrong..
It must be
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-+/]+)$ index.php?search=$1
RewriteRule ^([a-zA-Z0-9-+/]+)/$ index.php?search=$1
and the code should be
if(isset($_GET['search'])) {
$url = $_GET['search'];
$x = rtrim($url,'/');
$x = explode('/',$url);
print_r($x);
echo($url);