Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts

    Strip URL Function

    I needed something that would help with search engines, and conformity in my database, but I just couldn't find anything simple enough. So I browsed around, grabbed the tld database list, made an array out of all the extensions (I think... I hope...), and then did a few string modifications. My original design was really flawed, then I realized a much simpler way of doing it :P.

    Anyways, I wouldn't mind anyone adding to this . It works so far, but if you find any flaws, please improve ^.^.

    Been through a few edits already. If you find any errors with the below code, lemme know.

    PHP Code:
    <?php
    function strip_domain($url) {
     
    $arr = array('.ac''.ac.cn''.ac.jp''.ac.uk''.ad.jp''.adm.br''.adv.br''.aero''.ag''.agr.br''.ah.cn''.al''.am''.am.br''.arq.br''.art.br''.as''.asn.au''.at''.ato.br''.au''.av.tr''.az''.ba''.be''.bel.tr''.bg''.bio.br''.biz''.biz.tr''.bj.cn''.bmd.br''.br''.by''.ca''.cc''.cd''.ch''.cim.br''.ck''.cl''.cn''.cng.br''.cnt.br''.co.at''.co.jp''.co.uk''.com''.com.au''.com.br''.com.cn''.com.eg''.com.hk''.com.mx''.com.ru''.com.tr''.com.tw''.conf.au''.cq.cn''.csiro.au''.cx''.cy''.cz''.de''.dk''.dr.tr''.dz''.ecn.br''.edu''.edu.au''.edu.br''.edu.tr''.ee''.eg''.emu.id.au''.eng.br''.es''.esp.br''.etc.br''.eti.br''.eu''.eun.eg''.far.br''.fi''.fj''.fj.cn''.fm.br''.fnd.br''.fo''.fot.br''.fr''.fst.br''.g12.br''.gb''.gb.com''.gb.net''.gd.cn''.ge''.gen.tr''.ggf.br''.gl''.gob.mx''.gov.au''.gov.br''.gov.cn''.gov.hk''.gov.tr''.gr''.gr.jp''.gs''.gs.cn''.gx.cn''.gz.cn''.ha.cn''.hb.cn''.he.cn''.hi.cn''.hk''.hk.cn''.hl.cn''.hm''.hn.cn''.hu''.id.au''.idv.tw''.ie''.il''.imb.br''.ind.br''.inf.br''.info''.info.au''.info.tr''.int''.is''.it''.jl.cn''.jor.br''.jp''.js.cn''.jx.cn''.k12.tr''.ke''.kr''.la''.lel.br''.li''.lk''.ln.cn''.lt''.ltd.uk''.lu''.lv''.ma''.mat.br''.mc''.md''.me.uk''.med.br''.mil''.mil.br''.mil.tr''.mk''.mn''.mo.cn''.ms''.mt''.mus.br''.mx''.name''.name.tr''.ne.jp''.net''.net.au''.net.br''.net.cn''.net.eg''.net.hk''.net.lu''.net.mx''.net.ru''.net.tr''.net.tw''.net.uk''.nl''.nm.cn''.no''.no.com''.nom.br''.not.br''.ntr.br''.nu''.nx.cn''.nz''.odo.br''.oop.br''.or.at''.or.jp''.org''.org.au''.org.br''.org.cn''.org.hk''.org.lu''.org.ru''.org.tr''.org.tw''.org.uk''.pk''.pl''.plc.uk''.pol.tr''.pp.ru''.ppg.br''.pro.br''.psc.br''.psi.br''.pt''.qh.cn''.qsl.br''.rec.br''.ro''.ru''.sc.cn''.sd.cn''.se''.se.com''.se.net''.sg''.sh''.sh.cn''.si''.sk''.slg.br''.sm''.sn.cn''.srv.br''.st''.su''.sx.cn''.tc''.tel.tr''.th''.tj.cn''.tm''.tmp.br''.tn''.to''.trd.br''.tur.br''.tv''.tv.br''.tw''.tw.cn''.ua''.uk''.uk.com''.uk.net''.us''.va''.vet.br''.vg''.wattle.id.au''.web.tr''.ws''.xj.cn''.xz.cn''.yn.cn''.yu''.za''.zj.cn''.zlg.br');

     
    $url strtolower($url);
     
    $url str_replace('https://'''$url);
     
    $url str_replace('http://'''$url);
     
    $url str_replace('www.'''$url);

     if (
    strpos($url'@') >= 1) {
      
    $url substr($urlstrpos($url'@') + 1);
     }

      if (
    strpos($url'/') >= 1) {
       
    $len strpos($url'/') - 1;
       
    $str str_split($url);
        for(
    $i 0;$i <= $len;$i++) {
         
    $url2 $url2.$str[$i];
        }
       
    $url $url2;
      }

     foreach (
    $arr as $value) {
      if(
    strchr($url$value) != false) {
       echo 
    $url;
        break;
      }
     }
    }
    ?>
    Last edited by Deacon Frost; 01-05-2010 at 09:11 AM.

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    You might want to look into parse_url to get the domain and filter_var to validate if its an email address
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    New to the CF scene
    Join Date
    Feb 2010
    Location
    Williamsburg, VA
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So if I understand correctly, you are just trying to extract the domain name from a URL or email address?

    This function should accomplish that without the need to specify all the TLDs. I also changed it to return the value instead of echo.

    Edit: I didn't use filter_var because it doesn't do what you'd expect it to do. For example, "bob@local" is a valid email addresses according to filter_var, but obviously isn't for our purposes.

    PHP Code:
    function strip_domain($url) {

        if (
    strpos($url'@') >= 1) {
            return 
    substr($urlstrpos($url'@') + 1);
        }

        
    $url parse_url($url);
        
    $url $url['host'];
        
    $url str_ireplace('www.'''$url);

        return 
    $url;

    Last edited by zulugrid; 02-26-2010 at 11:47 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •