note 95289 added to function.mb-check-encoding

From: Date: Thu, 24 Dec 2009 12:52:57 +0000
Subject: note 95289 added to function.mb-check-encoding
Groups: php.notes 
Request: Send a blank email to php-notes+get-164298@lists.php.net to get a copy of this message
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(): 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 ---- Server IP: 69.147.83.197 Probable Submitter: 87.3.60.42 ---- Manual Page -- http://www.php.net/manual/en/function.mb-check-encoding.php Edit -- https://master.php.net/note/edit/95289 Del: integrated -- https://master.php.net/note/delete/95289/integrated Del: useless -- https://master.php.net/note/delete/95289/useless Del: bad code -- https://master.php.net/note/delete/95289/bad+code Del: spam -- https://master.php.net/note/delete/95289/spam Del: non-english -- https://master.php.net/note/delete/95289/non-english Del: in docs -- https://master.php.net/note/delete/95289/in+docs Del: other reasons-- https://master.php.net/note/delete/95289 Reject -- https://master.php.net/note/reject/95289 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#164298) next »