Welcome to WebmasterWorld Guest from 220.127.116.11 , register , login , search , subscribe , help , library , PubCon , announcements , recent posts , open posts Pubcon Website
Encoding a URL url encode immatt msg:3989705 2:32 pm on Sep 15, 2009 (gmt 0) I am trying to make an input that when submitted it will search google. My script looks like this
$input = 'url encoding';
$gsearch = urlencode($input);
print '<a href="http://www.google.com/#hl=en&q='.$gsearch.'">Google Search</a>';
The link will end up being:
(I am testing it on my computer)
Would I have to us preg_replace to take out the "http://localhost/" or is there some other way to do that?
rainborick msg:3989772 4:31 pm on Sep 15, 2009 (gmt 0)
You're on the right track. I would suggest that you do your best to encode any space characters in the search string as '+' instead of '%20' to make the request look more natural to Google. If Google starts to see your code as making automated queries, they will block the requests. StupidScript msg:3989959 10:50 pm on Sep 15, 2009 (gmt 0)
I have never seen urlencode add the protocol and host to the data it is encoding. urlencode simply makes whatever is passed to it "friendly" for use, by replacing spacebars with + signs, etc.
If you need to strip out the host name, use
$_SERVER['REQUEST_URI'] [ us2.php.net].