Escaping Javascript in PHP

后端 未结 1 442
孤城傲影
孤城傲影 2021-01-28 02:29

I\'m looking for the best way to escape some Javascript text in PHP, and json_encode is the wrong tool for the job.

The problem comes from this line:

1条回答
  •  日久生厌
    2021-01-28 02:57

    json_encode is the right tool for the job. Your problem arises from the fact that you are also including that Javascript in an HTML attribute, thus it also needs to be htmlspecialchars-encoded.

    echo " onclick=\"SwitchDiv(" . htmlspecialchars(json_encode($option)) . ")\"";
    

    0 讨论(0)
提交回复
热议问题