我对PHP的理解是,您可以使用“\n”或“\r\n”或echo“
创建新行。 但是我的应用程序并没有创建新的行。
”;
我在这里做错了什么?
下面是代码:
null
<?php
session_start(); // before any HTML is echoed
if($_POST) {
//$email = "";
$email = $_POST['email'];
$password = $_POST['password'];
if(isset($_POST['email'])) {
$email = str_replace(array("\r", "\n", "%0a", "%0d"), '', $_POST['email']);
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
}
if(isset($_POST['password'])) {
$password = htmlspecialchars($_POST['password']);
}
$recipient = "myemail@domain.com";
$headers = 'MIME-Version: 1.0' . "\r\n"
.'Content-type: text/html; charset=utf-8' . "\r\n"
.'From: ' . $email . "\r\n";
$email_content .= "Email: $email" . "\r\n";
echo "<br />\n";
$email_content .= "Password: $password";
echo $email_content;
if(mail($recipient, $email_content, $headers)) {
header("Location: default-image.png");
echo " <script language=javascript>
//alert('Done, Click Ok');
window.location='default-image.png';
</script>";
} else {
echo '<p>ERROR! Please go back and try again.</p>';
}
} else {
echo '<p>Something went wrong</p>';
}
?>
null
谢谢你的时间和投入。
将这些值赋给一个变量。 echo
不在该上下文中,它将把值输出到页面输出中。
$email_content .= "Email: $email" . "\r\n";
$email_content .= "<br />\n";
$email_content .= "Password: $password";
echo $email_content;
那才是正确的方式。
接下来,
是HTML中新行的表示形式。 \n
和\r\n
是新行的ASCII表示形式。 这主要用于文本文件和其他编辑器,例如CSV文件。 所以你把不同的东西混在一起。
DR
使用\n清理代码。 使用
添加换行。 确保将
连接到您的变量。
\n将清理源代码视图,并且需要将
连接到您的变量。
$email_content .= "Email: $email" . "<br />\n";
$email_content .= "Password: $password";
请注意,如果不包含\n,在浏览器中查看源代码时,可能会看到html位于同一行。
<?php
$email = "foo";
$password = "bar";
$email_content1 = "";
$email_content2 = "";
//without the \n to cleanup the source code
$email_content1 .= "Email: $email" . "<br />";
$email_content1 .= "Password: $password";
echo $email_content1;
//ignore, used for break
echo "\n\n<br />\n\n";
//with the \n to cleanup the source code
$email_content2 .= "Email: $email" . "<br />\n";
$email_content2 .= "Password: $password";
echo $email_content2;
?>
源代码如下所示:
Email: foo<br />Password: bar
<br />
Email: foo<br />
Password: bar
对于将在输出中显示的新行,应使用“\n”
$email_content .= "Email: $email" . "\n\n\n";
$email_content .= "\n\n";
$email_content .= "Password: $password";
echo $email_content;
您将看到密码以新行开头