Use json_encode()to convert PHP values ββto Javascript literal:
var text = <?php echo json_encode($text) ?>;
This works because the JSON format is a subset of the Javascript syntax. It will correctly indicate the strings and escape characters to be escaped.
source
share