PHP function to extract components from an URL

PHP function to extract components from an URL

You want to extract the protocol, domain name, path, or other significant component of a URL. Use the parse_url() function to split the URL into constituent parts automatically. The parse_url() function is one of PHP’s most helpful URL manipulation functions.

Ita takes a parameter of a Uniform Resource Locator (URL), and splits it into its components. The resulting associative array contains separate keys for the protocol, hostname, port number, remote path, and  GET  arguments. You can then easily access and use these keys for further processing.

// Define URL 
$url = "http://www.yourdomain.com:80/mobile-css/jquery/blog/article.php?id=179&page=5"; 

// Parse URL into associative array 
$data = parse_url($url); 

// print URL components foreach ($data as $k=>$v) { 
  echo "$k: $v \n"; 
}

Consider the output of the previous script, which illustrates this:

scheme: http

host: www.yourdomain.com

port: 80

path: /mobile-css/jquery/blog/article.php

query: id=179&page=5

Enjoy! Was this information helpful? What other tips would you like to read about in the future? Share your comments, feedback, and experiences with us by commenting below!

Total 0 Votes
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Comment

Back To Top