Recently I am working on a sky map for GIS integration. It is necessary to output HTML to JavaScript. It involves code escaping. It is quite troublesome. So I wrote a PHP function
to share:
function jsformat($str)
{
$str = trim($str);
$str = str_replace('\s\s', '\s', $str);
$str = str_replace(chr(10), '', $str);
$str = str_replace(chr(13), '', $str);
$str = str_replace(' ', '', $str);
$str = str_replace('\', '\\', $str);
$str = str_replace('"', '\"', $str);
$str = str_replace('\'', '\\'', $str);
$str = str_replace("'", "'", $str);
return $str;
}
Needless to say how to use it...just call jsformat($str) directly