php中的utf8编码

55ooxyrt  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(494)

此问题已在此处找到答案

utf-8全程(16个答案)
昨天关门了。
我从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: \n\n 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 在我最后一次尝试中,但仍然存在问题。

ruoxqz4g

ruoxqz4g1#

您需要解决程序输入和输出阶段可能出现的问题:
确保浏览器知道您想要utf-8:它需要标签 <meta charset="utf-8"> 在世界的某个地方 <head> 你的html。否则,在将表单数据发送回您时,它可能会退回到一些遗留编码。
确保电子邮件客户知道您向他们发送了utf-8。他们也将回到传统编码,而不明确告诉他们。尝试将以下标题添加到邮件中:

Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

如果您想在主题和/或to字段中使用utf-8,则需要单独的引用方式。我建议您看看优秀的phpmailer包,它将为您处理所有这些。

相关问题