detect utf8 string


/ Published in: PHP
Save to your folder(s)

I found this snippet in the FirePHP.class.php from www.firephp.org


Copy this code and paste it in your HTML
  1. /**
  2.   * Returns true if $string is valid UTF-8 and false otherwise.
  3.   *
  4.   * @param mixed $str String to be tested
  5.   * @return boolean
  6.   */
  7. protected static function is_utf8($str) {
  8. $c=0; $b=0;
  9. $bits=0;
  10. $len=strlen($str);
  11. for($i=0; $i<$len; $i++){
  12. $c=ord($str[$i]);
  13. if($c > 128){
  14. if(($c >= 254)) return false;
  15. elseif($c >= 252) $bits=6;
  16. elseif($c >= 248) $bits=5;
  17. elseif($c >= 240) $bits=4;
  18. elseif($c >= 224) $bits=3;
  19. elseif($c >= 192) $bits=2;
  20. else return false;
  21. if(($i+$bits) > $len) return false;
  22. while($bits > 1){
  23. $i++;
  24. $b=ord($str[$i]);
  25. if($b < 128 || $b > 191) return false;
  26. $bits--;
  27. }
  28. }
  29. }
  30. return true;
  31. }

URL: www.firephp.org

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.