Syntax:parse_url ($url, $component = -1)
Parameters:this function takes two parameter as above and described below:
- url:this parameter contains the URL to parse. Invalid characters are replaced with _ (underscore).
- component:this parameter specifies any of the components (PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URLUR_QURMENT or PHP_URL_QURMENT) a specific URL in the form of a string.
Returned values: - It returns an associative array if the component parameter is omitted.
- Returns a string if a component parameter is specified.
- Returns false if the parameter has an invalid URL.
The following are examples to illustrate the use of the parse_url ( ) in PHP:
Example 1:
// Declare a variable and initialize its URL
$url
=
’ http://engineerforengineer.org/php/#basics ’
;
// Use the parse_url() function to parse the URL
var_dump (
parse_url
(
$url
));
var_dump (
parse_url
( $url
, PHP_URL_SCHEME));
?>
Exit:array (4 ) {["scheme"] = > string (4) "http" ["host"] = > string (17) "engineerforengineer.org" ["path"] = > string (5) "/ php /" ["fragment"] = > string (6) "basics"} string (4) "http"
Example 2:
// Declare a variable and initialize its URL
$url
=
’//www.engineerforengineer .org / path? php = PHP’
;
// Use the parse_url() function for
// parse the URL
var_dump (
parse_url
(
$url
));
?>
Exit:array (3 ) {["host"] = > string (21) "www.engineerforengineer.org" ["path"] = > string (5) "/ path" ["query"] = > string (7) "php = PHP"}
Link: http://php.net/manual/en/function.parse-url.php