我对PHP的理解是,你可以使用""或"\r"或echo "<br>";
来创建新行。但是我对它们的应用根本无法创建新行。
我在这里做错了什么?
这是代码:
<?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' . "rn"
.'Content-type: text/html; charset=utf-8' . "rn"
.'From: ' . $email . "rn";
$email_content .= "Email: $email" . "rn";
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>';
}
?>
感谢您的时间和投入。
将值分配给变量。echo
不在该上下文中,会将您的值输出到页面输出中。
$email_content .= "Email: $email" . "rn";
$email_content .= "<br />n";
$email_content .= "Password: $password";
echo $email_content;
这才是正确的方式。
接下来是<br />
是 HTML 中新行的表示形式。n
和rn
是新行的 ASCII 表示形式。例如,这主要用于文本文件和其他编辑器或 CSV 文件。所以你混淆了不同的东西。
tl;博士
使用 清理代码。 使用<br />
添加换行符。 确保将<br />
串联到变量。
将清理源代码视图,并且需要将<br />
连接到变量。
$email_content .= "Email: $email" . "<br />n";
$email_content .= "Password: $password";
请注意,如果未包含 ,则在浏览器中查看源代码时,您可能会看到 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 "nn<br />nn";
//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
您应该使用""作为它将在输出中显示的新行
$email_content .= "Email: $email" . "nnn";
$email_content .= "nn";
$email_content .= "Password: $password";
echo $email_content;
您将看到密码以新行开头