PHP: Conditional Charset conversion to UTF-8

Here we will give you code snippets or tips related to PHP/MySQL/XHTML/CSS/JavaScript/AJAX development
Forum rules
Be civilized... # Seja civilizado... # Être civilisé...

PHP: Conditional Charset conversion to UTF-8

Postby weblivehelp » Sun Dec 14, 2008 2:46 pm

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: áêõ€Äíç");
?>
weblivehelp
Site Admin
 
Posts: 16
Joined: Thu May 01, 2008 3:23 pm

Re: PHP: Conditional Charset conversion to UTF-8

Postby matt » Mon Dec 15, 2008 7:34 pm

Great, I was looking for this! I found a lot of good stuff here in your forum. Keep up the good work!!
matt
 


Return to Web Development

Who is online

Users browsing this forum: No registered users and 1 guest