Example: If I wanted to make a call to Google's site search, I can currently do it like this: http://www.google.com/search?q=site:".$val['redirecturl']." This will give me: http://www.google.com/search?q=site:http://www.cnn.com I want it to make the call like this: http://www.google.com/search?q=site:www.cnn.com or even this: http://www.google.com/search?q=site:cnn.com Is there a generic piece of code I can add to my variable ".$val['redirecturl']." to do this? Thanks in advance, ED
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."
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
<?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
<?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,
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:
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 .
yeah but it'll take it out of the "http://google.com", too you just wanted it from the second url, right?