开发者

How can i pass a string with multiple line to a javascript function?

开发者 https://www.devze.com 2023-03-02 17:32 出处:网络
I have a variable in php $test = \"This is a string It has multiple lines there are three total\" ; (Output from a text area)

I have a variable in php

$test = "This is a string
It has multiple lines
there are three total" ;

(Output from a text area)

I want to pass this variable to a javascript function

I have write this in my php file

<?php echo "<script language=javascript>convert('$test');</script>"; ?>

but this makes an error "Unterminated st开发者_运维百科ring literal" How can i solve this issue ??


Try

$test = "This is a string\nIt has multiple lines\nthere are three total";


<script>convert(<?php echo json_encode($test); ?>);</script>


Try this:

$test = str_replace($test, "\n", "\\n");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号