Given this PHP code:
\'\"
What if there is a \' in $url?
\'
json_encode will work. You just have to use it the right way:
json_encode
This will work since json_encode already returns an JavaScript expression with quotes. And htmlspecialchars is needed to escape possible HTML meta characters.
htmlspecialchars