php中的Utf8编码



我从html表单中获得一些数据,并通过电子邮件与PHP发送它们。问题是我得到奇怪的字符,因为用户在表单中写的语言是捷克语。

例如名称Anežka变成Anežka

这是我的代码为我的php被用来发送电子邮件。

<?php 
if(isset($_POST['submit'])){
$to_alex = "myemail@honeywell.com"; // this is your Email address
$first_name = $_POST['fname'];
$first_name=mb_convert_encoding($first_name, "UTF-8", "ISO-8859-1");
$last_name = $_POST['lname'];
$email = $_POST['email'];
$skola = $_POST['vysoka_skola'];
$obor = $_POST['obor'];
$prace = $_POST['prace'];
$phone_num=$_POST['phone_number'];
$linkedin=$_POST['linkedin'];
$oponenta=$_POST['oponenta'];
$vedouciho=$_POST['vedouciho'];
$files = $_FILES['myfile']['name'];
$kategorie = $_POST['kategorie'];
//echo gettype($files);
$all_thesis_string="";
$all_vedouciho_string="";
for($index=0;$index<count($_FILES['myfile']['name']);$index++){
$all_thesis_string.=$_FILES['myfile']['name'][$index].","; //create a string with all bachelor attachments of user
}
for($index=0;$index<count($_FILES['vedouciho_files']['name']);$index++){
$all_vedouciho_string.=$_FILES['vedouciho_files']['name'][$index].","; //create a string with all vedouciho attachments of user
}
for($index=0;$index<count($_FILES['oponenta_files']['name']);$index++){
$all_vedouciho_string.=$_FILES['oponenta_files']['name'][$index].","; //create a string with all oponenta attachments of user
}
$subject = "Form submission of ".$first_name." ".$last_name;
$message = "User Details: nn Jméno: $first_name n Příjmení: $last_name n Email: $email n Telefon: $phone_num n Linkedin: $linkedin n Vysoká škola: $skola n Studijní obor: $obor n Odkaz na bakalářskou práci: $prace n Kategorie: $kategorie n Posudek oponenta: n $oponenta n Posudek vedouciho: n $vedouciho n Bakalářská práce: $all_thesis_string n Vedouciho files: $all_vedouciho_string";
$headers = "From:" . $first_name;
mail($to_alex,$subject,$message,$headers);
echo '<span style="color:#AFA;text-align:center;"><strong>"Přihláška úspěšně odeslána. Děkuji "</strong></span>';
//include 'upload.php';
// You can also use header('Location: thank_you.php'); to redirect to another page. 
}
?>

我在最后一次尝试中尝试了mb_convert_encoding,但仍然存在问题。

您需要处理程序输入和输出阶段可能出现的问题:

  1. 确保浏览器知道您想要UTF-8:它需要在HTML的<head>中的某个地方标记<meta charset="utf-8">。否则,当将表单数据发送回给您时,可能会退回到一些遗留编码。

  2. 确保,电子邮件客户端知道,您发送UTF-8给他们。它们也会在没有明确告诉它们的情况下退回到遗留编码。尝试将这些标题添加到邮件中:

    Mime-Version: 1.0
    Content-Type: text/plain; charset=utf-8
    Content-Transfer-Encoding: quoted-printable
    
  3. 如果您希望在主题和/或To字段中使用UTF-8,则需要单独的引用方式。我建议你看看出色的PHPMailer包,它将为你处理所有这些。

最新更新