PHP - Convert Special Characters to HTML Entities

2019-01-29 03:19发布

问题:

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:

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

How can I do this? Thanks.

回答1:

htmlentities() is what you're looking for:

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



回答2:

You are probably looking for htmlentities()



回答3:

htmlentities() does this.

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.



回答4:

Two issues:

(1) Use the htmlentities() located at http://php.net/manual/en/function.htmlentities.php

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)

The "UTF-8" forces a UTF-8 char-set (important, read below)

(2) Force a charset on BOTH the form page and the submit-to page.

Just below your opening php brackets insert the following:

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.