Strip URL to its domain name...


/ Published in: PHP
Save to your folder(s)

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'


Copy this code and paste it in your HTML
  1. function striptodomain($url) {
  2. if ((substr_count($url, 'http://www.')) > 0) {
  3. $url = str_replace('http://www.', '', $url);
  4. } elseif ((substr_count($url, 'http://')) > 0) {
  5. $url = str_replace('http://', '', $url);
  6. } elseif ((substr_count($url, 'www.')) > 0) {
  7. $url = str_replace('www.', '', $url);
  8. }
  9. if ((substr_count($url, '/')) > 0) {
  10. $url = substr($url, 0, strrpos($url, '/'));
  11. }
  12. return $url;
  13. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.