简体   繁体   English

从字符串中删除HTML标记和代码

[英]Remove html tags and codes from string

Hi I have this code in the database. 嗨,我在数据库中有此代码。 How can I retrieve it and send it as text through php mail()? 如何检索它并通过php mail()以文本形式发送? I have tried strip_lashes and it removed the <p> and other html tags but the html codes like &#39; 我尝试了strip_lashes,它删除了<p>和其他html标记,但是html代码如&#39;一样&#39; still persists even when I use php html_entity_decode... 即使我使用php html_entity_decode仍然仍然存在...

<p>Thank you somebody&#39;s favourite .</p><p><br /><p>Tel: &nbsp12345678/ 8881456</p>

You can use the strip_tags() function to remove HTML tags, and the html_entity_decode() function to replace HTML entities with their respective characters. 您可以使用strip_tags()函数删除HTML标记,并使用html_entity_decode()函数将HTML实体替换为其各自的字符。

$string = "<p>Thank you somebody&#39;s favourite .</p><p><br /><p>Tel: &nbsp12345678/ 8881456</p>";
$formatted = strip_tags(html_entity_decode($string));

Outputs: 输出:

Thank you somebody's favourite .Tel:  12345678/ 8881456

You can retrieve the message from the database and store it as it is in the $message variable then your mail function should be like this: 您可以从数据库中检索消息并将其按原样存储在$ message变量中,然后您的邮件功能应如下所示:

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

With the $headers variable like this: 使用$ headers变量,如下所示:

$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";

(The $headers variable should be defined before the mail function) ($ headers变量应在mail函数之前定义)

Hope it works. 希望它能工作。

You can use the PHP strip_tags function : 您可以使用PHP strip_tags函数:

http://php.net/manual/en/function.strip-tags.php http://php.net/manual/zh/function.strip-tags.php

只需将mime类型用作HTML的邮件功能,它将把您的文本视为HTML内容,用于发送邮件和在您提供的结构上发送。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM