PHP: How should I escape a string that will be going into a Javascript String?

后端 未结 4 807
南笙
南笙 2021-01-28 20:50

How should I escape a string that will be going into a Javascript String? URLEncode(X)? str_replace(\"\'\",\"\\\'\",X)?

4条回答
  •  臣服心动
    2021-01-28 21:29

    Use json_encode if available (since PHP 5.2):

    var str = ;
    

    Otherwise use you can use rawurlencode and decode it with decodeURIComponent:

    var str = decodeURIComponent("");
    

提交回复
热议问题