Welcome to WebmasterWorld Guest from 54.158.55.251

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Encoding a URL

url encode

     
2:32 pm on Sep 15, 2009 (gmt 0)

New User

5+ Year Member

joined:Aug 7, 2009
posts:6
votes: 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:

[google.com...]

(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 Sept 15, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2003
posts:940
votes: 27


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 Sept 15, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 20, 2004
posts:1477
votes: 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].

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members