如何处理 Javascript 中的换行符?(来自 PHP)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4270665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle newlines in Javascript? (from PHP)
提问by Zeno
I have code like this:
我有这样的代码:
<?php
echo '<script type="text/javascript">';
echo 'var out="'.$txt.'";';
echo '</script>';
?>
Where $txt is a PHP variable that can contain newlines like this:
其中 $txt 是一个 PHP 变量,可以包含这样的换行符:
line1
line2 hello world
Which would end up like this:
最终会是这样的:
var out="line1
line2 hello world";
Which will cause a Javascript error, of course.
当然,这会导致 Javascript 错误。
What is the best way to handle this? The out variable will be used in a HTML textarea, so I don't think it can be parsed into <br>
处理这个问题的最佳方法是什么?out 变量将在 HTML textarea 中使用,所以我认为它不能被解析为<br>
回答by zzzzBov
$txt = str_replace( array( "\n", "\r" ), array( "\n", "\r" ), $txt );
should replace newlines. Don't do it this way.
应该替换换行符。不要这样做。
This is a naïve implementation of string escaping for JavaScript. As you're actuallytrying to format a string for use in JavaScript, a much better solution would be to use json_encode
:
这是 JavaScript 字符串转义的简单实现。当您实际上尝试格式化字符串以在 JavaScript 中使用时,更好的解决方案是使用json_encode
:
$txt = json_encode($txt);
echo "<script>var out={$txt};</script>";
json_encode
will correctly escape special characters in strings, such as quotes, tabs, form feeds, and other special unicode characters. It will also perform all the correct escaping for converting objects, arrays, numbers, and booleans.
json_encode
将正确转义字符串中的特殊字符,例如引号、制表符、换页符和其他特殊的 unicode 字符。它还将为转换对象、数组、数字和布尔值执行所有正确的转义。
回答by Daniel
you can add a \
at the end of a line to create a multi line String
您可以\
在一行的末尾添加一个以创建多行字符串
var out="line1 \
line2 hello world";
回答by Dave Kiss
You can use str_replace
to convert line breaks into a different character (in this case, perhaps a space, but it depends how you want the output to show up)
您可以使用str_replace
将换行符转换为不同的字符(在这种情况下,可能是一个空格,但这取决于您希望输出的显示方式)
$out = str_replace("\n", '\n', $in);
回答by Dinesh Patra
I tried this and it worked well.
我试过了,效果很好。
<?php
echo '<script type="text/javascript">';
$txt = "line1 \n line2 hello world";
echo 'var out="'.$txt.'";';
echo '</script>';
?>
I am using PHP 5.3
我正在使用 PHP 5.3
回答by Roman
$content = str_replace( "\n", "\\\n", $content );
Result:
结果:
var a = "Hello \
World"
回答by MAtkins
Most of these don't work for me. Normally, I'd use json_encode like
大多数这些都不适合我。通常,我会使用 json_encode 之类的
<?php
$MyVar = json_encode($MyVar);
?>
<javascript language='javascript'>
MyVar = <?php echo $MyVar; ?>
But for a quick fix you can just break a line like this: Pay attention to the double quotes.
但是为了快速修复,您可以像这样中断一行:注意双引号。
<?php
$MyVar = "line one here
then line two here
finally line five here";
//** OR
$MyVar = $MyVarA .
"
"
. $MyVarB;
?>
<HTML>
<HEAD>
<javascript language='javascript'>
Myvar = "<?php echo $MyVar; ?>";
. . .
. . .