/ Published in: PHP
This is a function i created to basically stip a url down to its domain name and nothing else. For eg. If you passed it any of the following urls it would return the same value. http://www.google.com/ www.google.com http://www.google.com/search?hl=en&q=jadeyo&btnG=Google+Search&meta= All of the above urls would return the same varibale of 'google.com'
Expand |
Embed | Plain Text
function striptodomain($url) { } } return $url; }
Comments
Subscribe to comments
You need to login to post a comment.

Or you can use this:
$url= $SERVER['HTTPHOST'];
Omg. How much of a retard do i feel now?! :P I Googled for an easy way of doing this and nothing came up so i created my own function, lol.
Actually. No. That will only get the domain name of the host. My function, stips down any url passed to it. For eg. i used my function to standardize any urls submitted by users.
Did you just want to contain the domain's information without the subdomain www? Some site's use www. as a subdomain so you may want to include that. It's a good script, and pretty speedy, here's an alternative if you would prefer:
function stripit ( $url ) { $url = trim($url); $url = pregreplace("/^(http:\/\/)(www.)/is", "", $url); $url = pregreplace("/\/.*$/is" , "" ,$url); return $url; }
Created a test for this and yours as well - btw, I just noticed that you have a problem for $url[3]. Don't know if you wanted to keep the sub-directory or not.
$url[0] = " http://www.google.com/test "; $url[1] = "http://www.google.com/and"; $url[2] = "http://web.google.com/bla"; $url[3] = "http://google.com/go/"; $url[4] = "www.google.com/jeep"; $url[5] = "google.com/bam"; $url[6] = "http://google.com/forget"; $url[7] = "mail.google.com"; $url[8] = "google.co.in"; $url[9] = "http://google.co.uk"; $url[10] = "http://mail.google.co.uk"; $url[11] = "google.co.uk/"; $url[12] = "http://mail.google.co.uk/";
foreach ( $url as $u ) { echo $u.""; echo striptodomain($u).""; echo stripit($u).""; }