【问题标题】:How to add UTF-8 support to PHP? [closed]如何为 PHP 添加 UTF-8 支持? [关闭]
【发布时间】:2025-12-03 14:40:01
【问题描述】:

如何向这个 PHP 文件添加 UTF-8 支持?这样文字就乱码了。 我尝试添加一些我在网上找到的代码,但它没有工作......

这是我的原始代码。问题是为 UTF-8 支持添加什么。

<?php
/*
This first bit sets the email address that you want the form to be submitted to.
You will need to change this value to a valid email address that you can access.
*/

$webmaster_email = "natalie@cakery.co.il";

/*
This bit sets the URLs of the supporting pages.
If you change the names of any of the pages, you will need to change the values here.
*/
$feedback_page = "contact.html";
$error_page = "thanks.html";
$thankyou_page = "thanks.html";

/*
This next bit loads the form field data into variables.
If you add a form field, you will need to add it here.
*/
$Name1 = $_REQUEST['Name1'] ;
$Name2 = $_REQUEST['Name2'] ;
$Phone = $_REQUEST['Phone'] ;
$email_address = $_REQUEST['email_address'] ;
$comments = $_REQUEST['comments'] ;

/*
The following function checks for email injection.
Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
*/
function isInjected($str) {
    $injections = array('(\n+)',
    '(\r+)',
    '(\t+)',
    '(%0A+)',
    '(%0D+)',
    '(%08+)',
    '(%09+)'
    );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str)) {
        return true;
    }
    else {
        return false;
    }
}

// If the user tries to access this script directly, redirect them to the feedback form,
if (!isset($_REQUEST['email_address'])) {
header( "Location: $feedback_page" );
}

// If the form fields are empty, redirect to the error page.
elseif (empty($email_address) || empty($comments)) {
header( "Location: $error_page" );
}

// If email injection is detected, redirect to the error page.
elseif ( isInjected($email_address) ) {
header( "Location: $error_page" );
}

// If we passed all previous tests, send the email then redirect to the thank you page.
else {
mail( "$webmaster_email", "Feedback Form Results",
  $comments, "From: $email_address" );
header( "Location: $thankyou_page" );
}
?>

【问题讨论】:

标签: php utf-8


【解决方案1】:

您想发送一封符合 UTF8 标准的电子邮件,那么我认为您可以尝试在发送电子邮件之前对您的 $comments 进行 UTF8 编码:

$comments = utf8_encode($comments);

但这不保证可以工作,因为这完全取决于您的应用程序(特别是收集这些 cmets 的部分)的工作方式、它已经使用的编码以及 $comments 的工作方式本身就建好了。

utf8_encode(和我的建议)基于 假设,您看到“乱码”的原因是您实际上是在 UTF-8 的幌子下发送 Latin1 编码;但您的问题可能会有所不同,而且无需查看 $comments(最好是在 $commentshex 转储),猜测是任何人都可以做的最好的事情。

另一种可能性(完全相反)是您将未经宣传的 UTF8 发送给将其解码为 ISO-8859-15 的人。在这种情况下,您应该正确宣传内容类型而不是重新编码内容,将proper extra headers 添加到mail() 函数。

我推荐 deceze 的链接,即使第二个链接看起来可能会将我的建议归类为“虚假承诺”:-)

更新

好的,让我们说正在运行

$Name1 = $_REQUEST['Name1'];

让你得到奇怪的字符。以上来自某种形式;您需要检查显示该表单的页面的编码。假设您在“Name1”字段中输入了值“Léon”。然后,如果您在上面添加说明

die(bin2hex($Name1));

接收页面应该终止并显示一个十六进制字符串。如果名称在 UTF8 中正确接收,则应该是 4cc3a96f6e。但是如果你运行的是 ISO-8859-15,你会看到4ce96f6e,这意味着你要么改变传输格式的编码(这样它就可以传输好的 UTF8)或者你必须utf8_encode输入:

$Name1 = utf8_encode($_REQUEST['Name1']);

当然,您可能还会收到陌生的十六进制代码:4c826f6e,也许吧。

如果你收到4cc3a96f6e还是不行,问题是不是在发送的形式或接收的PHP,但后来。例如,当您显示字符串时。 那个页面可能采用 ISO-8859-15 编码,输出良好的 UTF8 将不起作用。如果是这种情况,您应该更改输出页面的广告编码,通过提供

Header('Content-Type: text/html; charset=UTF-8');

如果该页面中的其他文本出现损坏,则表示该页面已由 ISO-8859-15 编辑器编辑;因此它包含 ISO-8859-15 特殊字符,您可以通过 ISO 编码正确看到这些字符,但在转换为 UTF-8 时不再显示。在这种情况下,您需要先转换文件,然后从现在开始使用 UTF 编辑器。

【讨论】:

  • 呃,我不知道该怎么做。这里的代码看起来不同: $Name1 = $_REQUEST['Name1'] ; $Name2 = $_REQUEST['Name2'] ; $Phone = $_REQUEST['Phone'] ; $email_address = $_REQUEST['email_address'] ; $cmets = $_REQUEST['cmets'] ;