简体   繁体   English

PHP-将特殊字符转换为HTML实体

[英]PHP - Convert Special Characters to HTML Entities

I have a problem with sending emails. 我在发送电子邮件时遇到问题。 If it contains special characters, it won't send. 如果包含特殊字符,它将不会发送。 I want to convert the special characters to HTML entities like this: 我想将特殊字符转换为HTML实体,如下所示:

" ==> "
& ==> &
€ ==> €
< ==> &lt;
....

How can I do this? 我怎样才能做到这一点? Thanks. 谢谢。

htmlentities() is what you're looking for: htmlentities()是您要寻找的:

http://uk3.php.net/manual/en/function.htmlentities.php http://uk3.php.net/manual/zh/function.htmlentities.php

您可能正在寻找htmlentities()

htmlentities() does this. htmlentities()执行此操作。

Use it like this: 像这样使用它:

$text = htmlentities($text);

But this should not be necessary if you provide proper charset information. 但是,如果您提供正确的字符集信息,则不必这样做。 Try setting the charset of your mail. 尝试设置邮件的字符集。

Two issues: 两个问题:

(1) Use the htmlentities() located at http://php.net/manual/en/function.htmlentities.php (1)使用位于http://php.net/manual/en/function.htmlentities.php的htmlentities()

Basic usages: 基本用法:

$clean = htmlentities($dirty, ENT_QUOTES, "UTF-8");

The "ENT_QUOTES" will result in both single and double quotes being converted (easy to change) “ ENT_QUOTES”将导致单引号和双引号都被转换(易于更改)

The "UTF-8" forces a UTF-8 char-set (important, read below) “ UTF-8”强制使用UTF-8字符集(重要,请参见下文)

(2) Force a charset on BOTH the form page and the submit-to page. (2)在表单页面和提交到页面上同时强制使用字符集。

Just below your opening php brackets insert the following: 在打开的PHP括号下方插入以下内容:

header('Content-Type: text/html; charset=utf-8');

It is important that you force a charset on both pages (realistically, on every page of your website.) 在两个页面上(实际上,在网站的每个页面上)都强制使用一个字符集很重要。

That should resolve the issues. 那应该解决问题。 If not, you have issues elsewhere within your purification system. 如果不是这样,则说明纯化系统中存在其他问题。

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

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