-3

この 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" );
}
?>
4

1 に答える 1

4

You want to send an UTF8 compliant email, then I think you might try and UTF8 encode your $comments before sending the email:

$comments = utf8_encode($comments);

But this isn't guaranteed to work because it all depends on how your application (and specifically the part gathering those comments) is working, what encoding it is already using, and how $comments itself is built.

utf8_encode (and my suggestion) operates on the assumption that the reason you're seeing "garbled characters" is that you're actually sending Latin1 encoding under the guise of UTF-8; but your problem might be different, and without having a look at $comments (best of all, at a hex dump of $comments), a guess is the best anyone can do.

Another possibility (exactly opposite) is that you're sending unadvertised UTF8 to somebody who decodes it as ISO-8859-15. In that case you should properly advertise the content type instead of re-encoding the content, adding the proper extra headers to mail() function.

I recommend deceze's links, even if the second might look as it classifies my suggestion among the "false promises" :-)

Update

Okay, so let's say that running

$Name1 = $_REQUEST['Name1'];

gets you strange characters. The above comes from some form; you need to check the encoding of the page displaying that form. Let's say that you have put in the "Name1" field the value "Léon". Then if you add, above, the instruction

die(bin2hex($Name1));

the receiving page ought to terminate and display a hex string. If the name was correctly received in UTF8, it ought to say 4cc3a96f6e. But if you're running ISO-8859-15, you will see 4ce96f6e, and that means that you need to either change the encoding of the transmitting form (so that it transmits good UTF8) or you have to utf8_encode the input:

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

Of course you might also receive stranger hex codes: 4c826f6e, maybe.

In case you receive 4cc3a96f6e and yet it still does not work, the problem is not in the transmitting form or the receiving PHP but later. For example when you display the string. That page might be in ISO-8859-15 encoding, and outputting good UTF8 will not work. If that it the case, you ought to change the advertised encoding of the output page, by supplying

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

If the other text in that page appears broken, this means that the page has been edited by a ISO-8859-15 editor; so it contains ISO-8859-15 special characters, that you see correctly with the ISO encoding, but no more when shifting to UTF-8. In that case you need to convert the file before everything else, and use an UTF editor from now on.

于 2012-11-02T22:39:53.537 に答える