带有新行“\ n”无法正常工作的php echo警告消息

时间:2015-04-23 08:17:18

标签: javascript php string

我想通过alert()显示一条消息

$message = "No result found for the following info:Name: ".$FullName."IC: ".$ID." in database.";
echo "<script>alert('".$message."'); window.history.back();</script>";

这是有效的但是如果我在消息中添加一行新的'\ n'

$message = "No result found for the following info:\nName: ".$FullName."\nIC: ".$ID." in database.";

它不会显示弹出消息。有什么问题?

5 个答案:

答案 0 :(得分:4)

将其编辑为不更改为PHP中的换行符,而是改为javascript:

'No result found for the following info:\nName: '.$FullName.'\nIC: '.$ID.' in database.'
^                                               ^           ^      ^     ^             ^

添加额外的反斜杠:"\\n"

据Panther说,也是事实:使用'alert("' . $message . '")'

答案 1 :(得分:3)

在换行符上添加另一个\反斜杠:

$message = "No result found for the following info:\\nName: ".$FullName."\\nIC: ".$ID." in database.";

Output

答案 2 :(得分:3)

或者使用PHP_EOL

 $msg = 'Hello' . PHP_EOL . 'Next line';

答案 3 :(得分:1)

\n等不是用单引号解释的,只是用双引号解释。

echo "<script>alert(\"" . $message . "\"); window.history.back();</script>";

OR

echo '<script>alert("' . $message . '"); window.history.back();</script>';

答案 4 :(得分:0)

您需要使用Line feed + Newline

\r\n

,它将如以下脚本所示工作:

echo "If you view the source of output frame \r\n you will find a newline in this string.";
echo nl2br("You will find the \n newlines in this string \r\n on the browser window.");