homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Encoding a URL
url encode

 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?



 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.


 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].

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved