php / javascript在querystring中转义引号的最佳方法

时间:2012-12-26 14:32:59

标签: php html-escape-characters

我使用ajax和php输出超链接,使用户能够通过电子邮件发送一些文本。该链接指向邮件脚本,查询字符串包含要邮寄的文本。通过单击链接,用户可以邮寄文本。但是,如果文本(也是用户创建的)包含引号,则此引号将出现在查询字符串中并中断代码。具体来说,浏览器认为查询字符串在遇到引号时结束。报价后的文字溢出到页面上。我可以将超链接括在撇号而不是引号中,但如果用户在文本中包含撇号,我会遇到同样的问题。

我试图使用json_encode来逃避这些特殊字符,但这不起作用。我知道有addlashes或者我可以用str_replace破解一些东西,但是我想以正确的方式做到这一点。

感谢您就正确的方法提出任何建议。

PHP

go to dbase

$text = $row['text'];

echo '<a href="sendmail.php?text='.$text.'">email text</a>';

/*
say for sake of argument text is 'the dimensions of the painting are 24" x 36"'

then above link is

echo '<a href="sendmail.php?text=the dimensions of the painting are 24" x 36"">email text</a>';
 which breaks code
*/

1 个答案:

答案 0 :(得分:2)

应该使用

urlencode,因为您正在对文本进行编码以将其放入网址中。