Page 1 of 1

PHP: Conditional Charset conversion to UTF-8

PostPosted: Sun Dec 14, 2008 2:46 pm
by weblivehelp
Hey there,

We know we've missed out November post, but we were just trying to see if someone posted more things.

We can see some topics are viewed by many people everyday (specially the one about Converting Objects to Arrays and vice-versa), and you don't have to be registered to post a reply, but still, you people don't participate much... :|

Anyway, here we provide you with two functions:

is_utf8() to verify if a string has characters that have utf8 charset encoding. (this was found in here: http://us2.php.net/manual/en/function.mb-detect-encoding.php#85294)

utf8() to encode a string ONLY IF the string isn't already encoded.

Code: Select all
<?php

function is_utf8($str) {
   $c=0; $b=0;
   $bits=0;
   $len=strlen($str);
   for($i=0; $i<$len; $i++){
      $c=ord($str[$i]);
      if($c > 128){
         if(($c >= 254)) return false;
         elseif($c >= 252) $bits=6;
         elseif($c >= 248) $bits=5;
         elseif($c >= 240) $bits=4;
         elseif($c >= 224) $bits=3;
         elseif($c >= 192) $bits=2;
         else return false;
         if(($i+$bits) > $len) return false;
         while($bits > 1){
            $i++;
            $b=ord($str[$i]);
            if($b < 128 || $b > 191) return false;
            $bits--;
         }
      }
   }
   return true;
}

function utf8($string) {
   if (is_utf8($string)) {
      return $string;
   } else {
      return utf8_encode($string);
   }
}

?>


So if you need to have a string utf8 encoded, use:

Code: Select all
<?php
echo utf8("Some text in ISO-8859-1: áêõ€Äíç");
?>

Re: PHP: Conditional Charset conversion to UTF-8

PostPosted: Mon Dec 15, 2008 7:34 pm
by matt
Great, I was looking for this! I found a lot of good stuff here in your forum. Keep up the good work!!