简体   繁体   English

为什么html标签出现在PHP的字符串中?

[英]Why are the html tags showing up in my string in PHP?

I'm using a mail function which is sending back a message that is contained within the variable $body. 我正在使用一个邮件功能,它发回一个包含在变量$ body中的消息。 I want to send the message to myself with certain words in bold and a number of break tags. 我希望用一些粗体字和一些中断标记将消息发送给自己。

Here is my code(HEREDOC SYNTAX): 这是我的代码(HEREDOC SYNTAX):

$body = <<<CTS
<h1><b>Order for $name on datevariable</b></h1><br /><br />

<b><u>Administrative Details</u></b><br />
<b>Employee ID:</b> $id<br />
<b>Cost Center:</b> $cost_center<br />
<b>Approved By:</b> $approved_by<br />
<b>Delivery Bldg:</b> $delivery_bldg<br />
<b>Delivery Contact Email:</b> $delivery_contact<br />
<b>Ext:</b> $del_contact_ext<br />
CTS;

For some reason when I receive the email, it looks like this: 出于某种原因,当我收到电子邮件时,它看起来像这样:

<h1><b>Order for James Important on datevariable</b></h1><br /><br />
<b><u>Administrative Details</u></b><br />
<b>Employee ID:</b> 213123<br />
<b>Cost Center:</b> 132123<br />
<b>Approved By:</b> Chris Seckla<br />
<b>Delivery Bldg:</b> 6<br />
<b>Delivery Contact Email:</b> test@email.com<br />
<b>Ext:</b> 56<br />

It fills in the variable values but for some reason ignores the html tags. 它填充变量值但由于某种原因忽略了html标记。 Can someone please tell me how to fix this? 有人可以告诉我如何解决这个问题吗?

Also, it is ignoring the break tags and only putting breaks when I leave a line of whitespace. 此外,它忽略了break标记,只留下了一行空格。 Any ideas on this? 有什么想法吗?

If this is an email, you have to set the mime type to text/html instead of text/plain. 如果这是一封电子邮件,则必须将mime类型设置为text / html而不是text / plain。 Otherwise your email reader will interpret the message as plain text and ignore all the html tags. 否则,您的电子邮件阅读器会将邮件解释为纯文本,并忽略所有html标记。

For example, lets say you are calling the mail function like this: 例如,假设您正在调用这样的mail功能:

mail( $to, $subject, $message, $headers )

In $headers , you'd need something along these lines (adjusted for your particular case, of course): $headers ,你需要这些内容(当然根据你的具体情况进行调整):

$headers = "From: webmaster@example.com\r\n";
$headers .= "Reply-To: webmaster@example.com\r\n";
$headers .= "Content-Type: text/html";

If you want to send both plain text and html email (so non-html readers can see a clean version of the email without all those tags), I would suggest taking a look at this tutorial for a more detailed explanation of multipart emails. 如果你想发送纯文本和HTML电子邮件(所以非HTML阅读器可以看到没有所有这些标签的电子邮件的干净版本),我建议看一下教程,以获得有关多部分电子邮件的更详细说明。

You have to set an additional header when sending from php for the email to display using html format: 从php发送电子邮件时,您必须设置一个额外的标题,以便使用html格式显示:

Content-Type: text/html; 内容类型:text / html; charset=whatever 字符集=什么

Make sure you send the header that states it is HTML... 确保发送标题为HTML的标题...

example: 例:

mail('email@domain.com', 'Subject',
'<html><body><b>Hello</b></body></html>',
"To: The Receiver <recipient@domain.com>\n" .
"From: The Sender <email@domain.com>\n" .
"MIME-Version: 1.0\n" .
"Content-type: text/html; charset=iso-8859-1"); 

如果您想使用PHP发送邮件,我建议您使用PEAR Mail_Mime包而不是普通邮件功能

为什么我的 php 页面显示错误<!--?php tags</div--><div id="text_translate"><p> 我正在尝试创建一个仅使用 PHP 的网页(不需要任何 HTML 元素)但是当我运行该页面时出现以下错误: <a href="https://i.stack.imgur.com/EbOUu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EbOUu.png" alt="在此处输入图像描述"></a></p><p> 这是我页面的代码:</p><pre> <script> <?php if(isset($_COOKIE['string']) and isset($_COOKIE['studentarrays'])){ $string = $_COOKIE['string']; $array = explode(",",$string); array_shift($array); $studentstring = $_COOKIE['studentarrays']; $studentarrays = explode(",",$studentstring); ?> var array = <?php echo $studentarrays?>; alert(array); <?php }?> </script></pre><p> <a href="https://i.stack.imgur.com/bea2C.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bea2C.png" alt="在此处输入图像描述"></a></p><p> 我做错了什么?</p><p> 编辑:正如有人指出的,我有一个 SESSION 变量,但没有启动 session,所以这部分错误现在已经修复。 另外,当我将我的代码复制到该网站时,我忘记包含 session 代码,很抱歉造成混淆。 感谢您的所有回复,看来我在这个页面上犯了一些错误所以我决定返回 go 并尽可能简化这个页面,删除我不需要的任何东西。 这是我的新代码:</p><pre> <script> <?php include"databaseconnection.php"; session_start(); if(isset($_COOKIE['studentarrays'])){ $studentstring = $_COOKIE['studentarrays']; $studentarrays = explode(",",$studentstring); $teacherid = $_SESSION['username']; }?> </script></pre><p> 现在这似乎没有错误地工作,但我不知道到底是什么导致了错误。 如果我弄清楚原始代码导致错误的那一部分,我会更新这篇文章,在那之前请忽略这个问题</p></div> - Why is my php page showing an error on the <?php tags

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的HTML片段显示在页面上并破坏了页面? 它与PHP相关吗? - Why are pieces of my HTML showing up on the page and breaking it? Is it PHP related? html标签以字符串形式显示在我的页面中 - html tags are showing in my page as string html代码显示在php字符串中 - html code showing up in php string 为什么我的php标签转换为html评论? - Why are my php tags converted to html comments? 为什么我的数组在php中显示为0? - Why are my arrays showing up as 0 in php? 为什么我的 php 页面显示错误<!--?php tags</div--><div id="text_translate"><p> 我正在尝试创建一个仅使用 PHP 的网页(不需要任何 HTML 元素)但是当我运行该页面时出现以下错误: <a href="https://i.stack.imgur.com/EbOUu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EbOUu.png" alt="在此处输入图像描述"></a></p><p> 这是我页面的代码:</p><pre> <script> <?php if(isset($_COOKIE['string']) and isset($_COOKIE['studentarrays'])){ $string = $_COOKIE['string']; $array = explode(",",$string); array_shift($array); $studentstring = $_COOKIE['studentarrays']; $studentarrays = explode(",",$studentstring); ?> var array = <?php echo $studentarrays?>; alert(array); <?php }?> </script></pre><p> <a href="https://i.stack.imgur.com/bea2C.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bea2C.png" alt="在此处输入图像描述"></a></p><p> 我做错了什么?</p><p> 编辑:正如有人指出的,我有一个 SESSION 变量,但没有启动 session,所以这部分错误现在已经修复。 另外,当我将我的代码复制到该网站时,我忘记包含 session 代码,很抱歉造成混淆。 感谢您的所有回复,看来我在这个页面上犯了一些错误所以我决定返回 go 并尽可能简化这个页面,删除我不需要的任何东西。 这是我的新代码:</p><pre> <script> <?php include"databaseconnection.php"; session_start(); if(isset($_COOKIE['studentarrays'])){ $studentstring = $_COOKIE['studentarrays']; $studentarrays = explode(",",$studentstring); $teacherid = $_SESSION['username']; }?> </script></pre><p> 现在这似乎没有错误地工作,但我不知道到底是什么导致了错误。 如果我弄清楚原始代码导致错误的那一部分,我会更新这篇文章,在那之前请忽略这个问题</p></div> - Why is my php page showing an error on the <?php tags PHP在浏览器中显示html标签 - Php showing html tags in browser 我的 HTML 都没有出现在我的 PHP 页面上 - None of my HTML is showing up on my PHP page 为什么我的Laravel应用将html标签输出为字符串 - Why does my Laravel app output html tags as string 为什么我的链接没有通过php电子邮件功能显示? - Why are my links not showing up via php email function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM