PHP wordwrap数组到Javascript返回错误

PHP wordwrap数组到Javascript返回错误,javascript,php,Javascript,Php,运行以下代码时出现控制台错误: <?php $str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scramble

运行以下代码时出现控制台错误:


<?php
$str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
$str = wordwrap($str,30,'\n');
?>

document.write('test' + '\n');
document.write( '<?php echo $str;?>' + '\n' );

</script>


正如@u_mulder所指出的,您必须将引号符号
转义为:

<?php
    $str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
    $str = str_replace("'", "\'", wordwrap($str,30,'\n'));
?>
<script>
    document.write('test' + '\n');
    document.write( '<?php echo $str;?>' + '\n' );
</script>

那么,生成的JavaScript是什么样子的呢?文本中的引号打断了您的代码,了解什么是转义。@u\u mulder我需要转义php和js?