如何将多行字符串传递给javascript函数?
问题描述:
我在PHP中的变量如何将多行字符串传递给javascript函数?
$test = "This is a string
It has multiple lines
there are three total" ;
(输出文本区)
我想这个变量传递给JavaScript函数
我在我的PHP文件写这篇
<?php echo "<script language=javascript>convert('$test');</script>"; ?>
但这造成一个错误“未终止的字符串文字”我该如何解决这个问题?
答
尝试
$test = "This is a string\nIt has multiple lines\nthere are three total";
答
<script>convert(<?php echo json_encode($test); ?>);</script>
+0
它看起来像甚至可以逃脱双引号。非常好的解决方案IMO! – groovenectar 2015-09-28 19:11:22
不相关的问题,但你应该使用'转换('$ test');“; ?> –
Gaurav
2011-05-03 11:36:43
使用'\ r \ n',Luke – zerkms 2011-05-03 11:36:50
@Gaurav:'