FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How do I escape an ampersand in a url?





aswapathy
I want to use file_get_contents to read a url, but it can't read the second arg because it seems to stop at the ampersand.
badai
i assume you are using php since you post here

you can use $_GET['xxx']

example for http://file_get_contents.php?parameter1=value1&parameter2=value2&parameter3=value3

$value1 = $_GET['parameter1'];
$value2 = $_GET['parameter2'];
$value3 = $_GET['parameter3'];
aswapathy
Using this code:
$url = "http://finance.yahoo.com/q/op?s=BJS&m=2008-05";
$content = file_get_contents($url);
The resulting content comes from this url:
"http://finance.yahoo.com/q/op?s=BJS"
sonam
Try to use single quotes:
Code:
$url = 'http://finance.yahoo.com/q/op?s=BJS&m=2008-05';


Sonam
rvec
sonam wrote:
Try to use single quotes:
Code:
$url = 'http://finance.yahoo.com/q/op?s=BJS&m=2008-05';


Sonam

that won't make a difference.
you could use some other sign and replace that by a '&' in the next script or use post instead of get.
Arnie
Not sure about this, but would it be possible to use & in the URL instead of &?
rvec
I don't think that would work.
how would you make this link to reply in this topic:
http://www.frihost.com/forums/posting.php?mode=reply&t=91355
by adding anything after the & it will see it as the variable name and say no such topic exists because $_GET['t'] is not set.
AftershockVibe
As noted on the PHP Manual page:
http://uk.php.net/function.file-get-contents

Perhaps you'll have better luck if you use urlencode() before passing your string to file_get_contents().

Code:
 $url = urlencode('http://finance.yahoo.com/q/op?s=BJS&m=2008-05');
$content = file_get_contents($url);
Star Wars Fanatic
You can try replacing the & with "%26" as that is the ASCII Hex code for &, it may work. It also might not.

And would not work, as & is the html representation of an &, it wouldn't translate in the url.
Arnie
Actually, in ordinary HTML documents you have to use & in links to get it through the W3C Validator. See for yourself here (check the link to Google, then check the source, then run the validator).

I'm not a PHP kind of guy so I wouldn't know if it works there, but it's certainly not unimaginable.
Related topics
Url Forwarding
Why use IE?
Not humor, but a play-by-forum game.
URL and stuff...
Sacred: Underworld
I NEED ALL YOUR HELP
Federal response to Katrina was faster than Hugo,
Keep domain name in URL space
how can i use the url: xxx.frih.net
URL redirectors and search engines
www.ask4inf.pt.vu just starting
[url]http://www.themesite.co.uk[/url]
Favorite games to play: ROOM Escape Games
Frihost's miraculous escape
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.