示例
$string = "Test Line 1
Test Line 2
Test Line 3";
echo str_replace(PHP_EOL,"/".PHP_EOL, $string );
实际上我期待输出是
测试线1 \
测试线2 \
测试线3 \
请帮我解决这个问题
答案 0 :(得分:1)
使用\\
代替"/"
<?php
$string = "Test Line 1
Test Line 2
Test Line 3";
$var=str_replace(PHP_EOL,"\\"."</br>", $string );
echo $var." \\"; // this append \ in last string
?>
答案 1 :(得分:1)
试试这个,
<强>编辑:强>
<?php
$string = "Test Line 1
Test Line 2
Test Line 3";
$str = str_replace(PHP_EOL,"\<br />", $string) . " \\";
echo $str;
?>
答案 2 :(得分:0)
如果只是为了让你可以将php值放入JS变量中,你可以使用json_encode()
,这会将新行改为\n
$string = "Test Line 1 Test Line 2 Test Line 3"; echo json_encode($string); // outputs(including quotes) "Test Line 1 \nTest Line 2 \nTest Line 3"