utf 8 - Special characters (utf-8) in PHP contact message -


i have contact form. when receive message, can not read because special characters shown bizarrly.

i saved file in utf-8 without bom, red , tried lot of variations subject not find right answer myself.

the contact form in html :

<meta charset="utf-8" /> <meta http-equiv="content-type" content="text/html; charset=utf-8" />    <form id="contactform" action="processform.php" method="post" accept-charset='utf-8'>     <div class="form-label label-name">nom</div>     <input type="text" name="name" id="name" />     <div class="form-label label-postcode">code postal</div>     <input type="text" name="postcode" id="postcode" />     <div class="form-label label-email">e-mail</div>     <input type="text" name="email" id="email" />     <div class="form-label label-subject">sujet</div>     <input type="text" name="subject" id="subject" />     <div class="form-label label-message">message</div>     <textarea rows="4" name="message" id="message"></textarea>     <input type="submit" id="send" name="button" value="envoyer" />            </form> 

my processform.php file (saved in utf-8 without bom) has following codes :

<?php  include_once $_server['document_root'] . '/securimage/securimage.php';  $securimage = new securimage();  if ($securimage->check($_post['verif']) == false) {   // code incorrect   // should handle error form processor doesn't continue    // or can use following code if there no validation or not know how   echo "le code de s&eacute;curit&eacute; indiqu&eacute; est incorrect.<br />";   echo "rafra&icirc;chissez la page et essayez &agrave; nouveau svp.";   exit; }  // clean input values foreach($_post $key => $value) {   if(ini_get('magic_quotes_gpc'))     $_post[$key] = stripslashes($_post[$key]);    $_post[$key] = htmlspecialchars(strip_tags($_post[$key])); }  // assign input values variables easy reference $name = $_post["name"]; $email = $_post["email"]; $subject = $_post["subject"]; $message = $_post["message"];  // test input values errors $errors = array(); if(strlen($name) < 3) {   if(!$name) {     $errors[] = "entrez votre nom svp.";   } else {     $errors[] = "au moins 3 caractères sont requis pour un nom.";   } } if(!$email) {   $errors[] = "entrez votre email svp."; } else if(!validemail($email)) {   $errors[] = "veuillez fournir une adresse valide svp."; } if(strlen($message) < 20) {   if(!$message) {     $errors[] = "entrez un message svp.";   } else {     $errors[] = "au moins 20 caractères sont requis pour un message.";   } }  if($errors) {   // output errors , die failure message   $errortext = "";   foreach($errors $error) {     $errortext .= "<li>".$error."</li>";   }   die("<span class='failure'>les erreurs suivantes sont survenues :<ul>". $errortext ."</ul></span>"); }  // remplacement de certains caractères spéciaux header('content-type: text/html; charset=utf-8');  // send email $to = "myemail@gmail.com"; $message = " from: $name  email: $email  subject: $subject message: $message"; $headers = "message west hungary website";  mail($to, $subject, $message, $headers);  // die success message die("<span class='success'>votre message &eacute;t&eacute; envoy&eacute; avec succ&egrave;s !</span>");  // function checks see if // email valid function validemail($email) {    $isvalid = true;    $atindex = strrpos($email, "@");    if (is_bool($atindex) && !$atindex)    {       $isvalid = false;    }    else    {       $domain = substr($email, $atindex+1);       $local = substr($email, 0, $atindex);       $locallen = strlen($local);       $domainlen = strlen($domain);       if ($locallen < 1 || $locallen > 64)       {          // local part length exceeded          $isvalid = false;       }       else if ($domainlen < 1 || $domainlen > 255)       {          // domain part length exceeded          $isvalid = false;       }       else if ($local[0] == '.' || $local[$locallen-1] == '.')       {          // local part starts or ends '.'          $isvalid = false;       }       else if (preg_match('/\\.\\./', $local))       {          // local part has 2 consecutive dots          $isvalid = false;       }       else if (!preg_match('/^[a-za-z0-9\\-\\.]+$/', $domain))       {          // character not valid in domain part          $isvalid = false;       }       else if (preg_match('/\\.\\./', $domain))       {          // domain part has 2 consecutive dots          $isvalid = false;       }       else if(!preg_match('/^(\\\\.|[a-za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',                  str_replace("\\\\","",$local)))       {          // character not valid in local part unless          // local part quoted          if (!preg_match('/^"(\\\\"|[^"])+"$/',              str_replace("\\\\","",$local)))          {             $isvalid = false;          }       }       if ($isvalid && !(checkdnsrr($domain,"mx") || checkdnsrr($domain,"a")))       {          // domain not found in dns          $isvalid = false;       }    }    return $isvalid; }  ?> 

since you're using utf-8, consider using php multibyte functions link mb_*. example:

echo strlen('è'); // output 2 echo mb_strlen('è', 'utf-8'); // output 1 

other mb_* functions might interested in:

strrpos() mb_strrpos()

substr() mb_substr()

for full list of multibyte functions, here.

if don't want pass 'utf-8' in every mb_ function, call mb_internal_encoding('utf-8') in beginning of script.

for mail() function work utf-8, pass following in header:

$header = "content-type: text/html; charset=utf-8" 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - How to Hide Date Menu from Datepicker in yii2 -