note 95289 deleted from function.mb-check-encoding by salathe

From: Date: Sat, 14 Aug 2021 09:08:16 +0000
Subject: note 95289 deleted from function.mb-check-encoding by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-216788@lists.php.net to get a copy of this message
Note Submitter: javalc6 at gmail dot com ---- In order to check if a string is encoded correctly in utf-8, I suggest the following function, that implements the RFC3629 better than mb_check_encoding(): <?php function check_utf8($str) { $len = strlen($str); for($i = 0; $i < $len; $i++){ $c = ord($str[$i]); if ($c > 128) { if (($c > 247)) return false; elseif ($c > 239) $bytes = 4; elseif ($c > 223) $bytes = 3; elseif ($c > 191) $bytes = 2; else return false; if (($i + $bytes) > $len) return false; while ($bytes > 1) { $i++; $b = ord($str[$i]); if ($b < 128 || $b > 191) return false; $bytes--; } } } return true; } // end of check_utf8 ?>

« previous php.notes (#216788) next »