1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need to strip http:// from url

Discussion in 'PHP' started by schmidte, Dec 25, 2007.

  1. #1
    schmidte, Dec 25, 2007 IP
  2. dancom96

    dancom96 Well-Known Member

    Messages:
    1,556
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    105
    #2
    Have you tried str_replace ?
     
    dancom96, Dec 25, 2007 IP
  3. gopher292

    gopher292 Peon

    Messages:
    64
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It looks like you're using PHP?:

    $temp=str_replace("http://", "", $val['redirecturl']); //to remove http...
    $temp=str_replace("www.", "", $val['redirecturl']); //to remove www.

    Replace the last argument in the str_replace() function with $temp if you want to remove both.
    Then:

    http://www.google.com/search?q=site:".$temp."
     
    gopher292, Dec 25, 2007 IP
  4. buldozerceto

    buldozerceto Active Member

    Messages:
    1,137
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    88
    #4
    Or
    $url="http://www.example.com";
    $tmp=explode('http://',$url);
    echo $tmp[1]; // prints www.example.com
    $tmp1=explode('www.',$tmp[1]);
    echo $tmp1[1]; // prints example.com
     
    buldozerceto, Dec 25, 2007 IP
  5. Meth_

    Meth_ Well-Known Member

    Messages:
    1,063
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    140
    #5
    
    <?php
    
    $url = 'http://www.google.com/search?q=site:http://www.cnn.com';
    
    //replace the http
    
    $url = str_replace('site:http://', 'site:', $url);
    
    ?>
    
    PHP:
    that will get the second http away from the string, but keep the original http for the google url
     
    Meth_, Dec 25, 2007 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #6
    
    <?php
    $url = 'http://www.google.com/search?q=site:http://www.cnn.com';
    $url = str_replace(array('site:http://www.','site:http://'), 'site:', $url);
    ?>
    
    PHP:
    Peace,
     
    Barti1987, Dec 25, 2007 IP
  7. dataman

    dataman Peon

    Messages:
    94
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    But what happens if there is "NO" www or it's not "www" or the domain name is 'www", you should do it the right way, not just hack something together that will break sooner or later!

    Use parse_url(), then explode the "host" on the ".", then work backward until you find the domain name, so you everything will work no matter the domain name or how many sub-TLN the domain has. Sure it more code, but will will always work!

    <?php
    
    function getDomain ( $host )
    {
    	if ( ( $x = substr_count ( $host, '.' ) ) <= 1 )
    	{
    			return $host;
    	}
    
    	$part = explode ( '.', $host );
    
    	$last = $part[$x];
    
    	$test = $part[--$x] . '.' . $last;
    
    	$list = 'ac.cn,ac.jp,ac.uk,ad.jp,adm.br,adv.br,agr.br,ah.cn,am.br,arq.br,art.br,asn.au,ato.br,av.tr,bel.tr,bio.br,biz.tr,bj.cn,bmd.br,cim.br,cng.br,cnt.br,co.at,co.jp,co.uk,com.au,com.br,com.cn,com.eg,com.hk,com.mx,com.ru,com.tr,com.tw,conf.au,cq.cn,csiro.au,dr.tr,ecn.br,edu.au,edu.br,edu.tr,emu.id.au,eng.br,esp.br,etc.br,eti.br,eun.eg,far.br,fj.cn,fm.br,fnd.br,fot.br,fst.br,g12.br,gb.com,gb.net,gd.cn,gen.tr,ggf.br,gob.mx,gov.au,gov.br,gov.cn,gov.hk,gov.tr,gr.jp,gs.cn,gx.cn,gz.cn,ha.cn,hb.cn,he.cn,hi.cn,hk.cn,hl.cn,hn.cn,id.au,idv.tw,imb.br,ind.br,inf.br,info.au,info.tr,jl.cn,jor.br,js.cn,jx.cn,k12.tr,lel.br,ln.cn,ltd.uk,mat.br,me.uk,med.br,mil.br,mil.tr,mo.cn,mus.br,name.tr,ne.jp,net.au,net.br,net.cn,net.eg,net.hk,net.lu,net.mx,net.ru,net.tr,net.tw,net.uk,nm.cn,no.com,nom.br,not.br,ntr.br,nx.cn,odo.br,oop.br,or.at,or.jp,org.au,org.br,org.cn,org.hk,org.lu,org.ru,org.tr,org.tw,org.uk,plc.uk,pol.tr,pp.ru,ppg.br,pro.br,psc.br,psi.br,qh.cn,qsl.br,rec.br,sc.cn,sd.cn,se.com,se.net,sh.cn,slg.br,sn.cn,srv.br,sx.cn,tel.tr,tj.cn,tmp.br,trd.br,tur.br,tv.br,tw.cn,uk.com,uk.net,vet.br,wattle.id.au,web.tr,xj.cn,xz.cn,yn.cn,zj.cn,zlg.br,co.nr,co.nz,';
    
    	if ( false === stristr ( $list, $test . ',' ) )
    	{
    		return $test;
    	}
    
    	$last = $part[--$x] . '.' . $test;
    
    	if ( false === stristr ( $list, $last . ',' ) )
    	{
    		return $last;
    	}
    
    	return $part[--$x] . '.' . $last;
    }
    
    $val = array ();
    
    $val['redirecturl'] = 'http://news.bbc.co.uk/';
    
    $parts = parse_url ( $val['redirecturl'] );
    
    echo "http://www.google.com/search?q=site:" . getDomain ( strtolower ( $parts['host'] ) ) . "\n";
    
    ?>
    PHP:
     
    dataman, Dec 25, 2007 IP
  8. schmidte

    schmidte Peon

    Messages:
    345
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks to all that replied.....gopher292's suggestion worked perfectly.
    Ed
     
    schmidte, Dec 25, 2007 IP
  9. coches

    coches Peon

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    but remember str_replace accepts also an array as argument for elements to replace , so you dont need to use 2 str_replaces ...
    as azizny outlined .
     
    coches, Dec 26, 2007 IP
  10. kendo1979

    kendo1979 Peon

    Messages:
    208
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    this page has helped me too :D
     
    kendo1979, Dec 26, 2007 IP
  11. Meth_

    Meth_ Well-Known Member

    Messages:
    1,063
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    140
    #11
    yeah but it'll take it out of the "http://google.com", too

    you just wanted it from the second url, right?
     
    Meth_, Dec 26, 2007 IP