开发者

PHP - Convert Special Characters to HTML Entities

开发者 https://www.devze.com 2023-02-06 17:40 出处:网络
I have a problem with sending emails.If it contains special characte开发者_如何学运维rs, it won\'t send. I want to convert the special characters to HTML entities like this:

I have a problem with sending emails. If it contains special characte开发者_如何学运维rs, it won't send. I want to convert the special characters to HTML entities like this:

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

How can I do this? Thanks.


htmlentities() is what you're looking for:

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


You are probably looking for htmlentities()


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消