json_encode
will work. You just have to use it the right way:
<a onclick="javascript:window.location.href=<?php echo htmlspecialchars(json_encode($url)); ?>">
This will work since json_encode
already returns an JavaScript expression with quotes. And htmlspecialchars
is needed to escape possible HTML meta characters.