开发者

Echoing variables that have ' in them

开发者 https://www.devze.com 2023-03-20 16:54 出处:网络
I have this echo, echo \"<a href = \'showdetails.php?name=\'$name\'&type=movie\'> </a>\";

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

精彩评论

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

关注公众号