dousha1831 2011-10-24 02:02
浏览 28
已采纳

从php构建javascript时转义引号

Basically, I'm taking user input and passing it to a javascript function in a page from php. But because the user use's apostrophes, I'm getting errors. What's the proper escape function in php to use on a variable that will be surrounded by quotes. IE:

Some php:

$userString = "Joe's Pizza";
// escape here
echo "<script type=\"text/javascript\">myFunction('$userString');</script>";

Thanks much!

  • 写回答

2条回答 默认 最新

  • dongyan2267 2011-10-24 02:04
    关注

    addslashes; e.g.

    $userString = addslashes("Joe's Pizza");
    print '<script type="text/javascript">myFunction('$userString');</script>";;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?