Filter url to domain

cwarn23 cwarn23 is offline Offline Sep 10th, 2009, 1:24 am |
1
In these three functions, you can filter to a url to just the domain name. There are three main methods of doing so which are all shown in the example and the usage of each function is shown in the example.
Quick reply to this message  
PHP Syntax
  1. <?php
  2. //function one - get entire domain
  3. function fulldomain($domainb) {
  4. $bits = explode('/', $domainb);
  5. if ($bits[0]=='http:' || $bits[0]=='https:')
  6. {
  7. return $bits[0].'//'.$bits[2].'/';
  8. } else {
  9. return 'http://'.$bits[0].'/';
  10. }
  11. unset($bits);
  12. }
  13.  
  14. //function two - use regex to get entire domain
  15. function preg_fulldomain ($domainb) {
  16. return
  17.  
  18. preg_replace('/^((http(s)?:\/\/)?([^\/]+)(\/)?)(.*)/','$1',$domain
  19.  
  20. b);
  21. }
  22.  
  23. //function three - get domain and remove subdomain.
  24. function domain($domainb)
  25. {
  26. $bits = explode('/', $domainb);
  27. if ($bits[0]=='http:' || $bits[0]=='https:')
  28. {
  29. $domainb= $bits[2];
  30. } else {
  31. $domainb= $bits[0];
  32. }
  33. unset($bits);
  34. $bits = explode('.', $domainb);
  35. $idz=count($bits);
  36. $idz-=3;
  37. if (strlen($bits[($idz+2)])==2) {
  38. $url=$bits[$idz].'.'.$bits[($idz+1)].'.'.$bits[($idz+2)];
  39. } else if (strlen($bits[($idz+2)])==0) {
  40. $url=$bits[($idz)].'.'.$bits[($idz+1)];
  41. } else {
  42. $url=$bits[($idz+1)].'.'.$bits[($idz+2)];
  43. }
  44. return $url;
  45. }
  46.  
  47.  
  48. //below is the usage of each function
  49. $address='http://www.subdomain.example.com/blog/index.php?id=1';
  50. echo fulldomain($address);
  51. echo '<br>';
  52. echo preg_fulldomain($address);
  53. echo '<br>';
  54. echo domain($address);
  55. ?>
2
ShawnCplus ShawnCplus is offline Offline | Sep 10th, 2009
 
0
NicAx64 NicAx64 is offline Offline | Sep 10th, 2009
You have used the preg instead of ereg I think it's a good Patrice .
 
1
whatsgoingon whatsgoingon is offline Offline | Oct 2nd, 2009
Agree that using parse_url could help. Thanks for writing this
Last edited by whatsgoingon; Oct 2nd, 2009 at 8:53 am.
 
 

Tags
domain, filter, function, url

Message:


Thread Tools Search this Thread



Tag cloud for domain, filter, function, url
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC