programming faqs interview questions tech technical educational freshers guide preparation interviews hr telephonic
try another color:
try another fontsize: 60% 70% 80% 90%
Programming FAQs

How to parse url and how to get its elements

using parse_url function we

using parse_url function we can get all components of url

$url = 'http://testurl.com/path?arg=value#anchor';
echo '';
print_r(parse_url($url));

o/p

Array
(
[scheme] => http
[host] => testurl.com
[path] => /path
[query] => arg=value&val=php&cat=technology
[fragment] => anchor
)

How to parse the query

How to parse the query string from above snippet.

$url = 'http://testurl.com/path?arg=value&val=php&cat=technology#anchor';
$p_url = parse_url($url);
Syntax : //void parse_str ( string $str [, array &$arr ] )
parse_str($p_url['query'],$op);

// or else you can get the query string using $op = $_SERVER['QUERY_STRING']
print_r($op);

o/p

Array
(
[arg] => value
[val] => php
[cat] => technology
)