Generating JavaScript strings

Well, I’ve always experienced the generating JavaScript strings with PHP as a PITA. An example, which requires you to take care of the escaping of quotes, is the string: ‘O’Reilly has nice books’. Today i had this brilliant idea to do it as following:

<?php
$str = addslashes("Hello peter's cats");
echo "<script type='text/javascript'>";
echo "alert('$str')";
echo "";
?>

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>