开发者

Echoing variables that have ' in them

I have this echo,

echo "<a href = 'showdetails.php?name='$name'&type=movie'> </a>";

that is working as intend, but if

$name = Gulliver's Travels;

for example, I will only receive Gulliver in the other page.

Is there any way of 开发者_开发问答circumventing this?

Thanks,

Zenshi.


There are many characters that don't belong in URLs, so encode them all properly:

echo '<a href="showdetails.php?name=' . urlencode($name) . '&type=movie"> </a>';

The href value should be enclosed in double quotes, not single quotes, since single quotes are allowed in URLs but double quotes aren't.

http://php.net/manual/en/function.urlencode.php

If you were putting this in any other type of HTML output than a link, you'd want to use htmlspecialchars.


I'd go for urlencode because you are using it as part of an URL:

echo "<a href = 'showdetails.php?name=".urlencode($name)."&type=movie'> </a>";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜