- function valid_1byte($char) {
- if(!is_int($char)) return false;
- return ($char & 0x80) == 0x00;
- }
-
- function valid_2byte($char) {
- if(!is_int($char)) return false;
- return ($char & 0xE0) == 0xC0;
- }
-
- function valid_3byte($char) {
- if(!is_int($char)) return false;
- return ($char & 0xF0) == 0xE0;
- }
-
- function valid_4byte($char) {
- if(!is_int($char)) return false;
- return ($char & 0xF8) == 0xF0;
- }
-
- function valid_nextbyte($char) {
- if(!is_int($char)) return false;
- return ($char & 0xC0) == 0x80;
- }
-
- function valid_utf8($string) {
- $len = strlen($string);
- $i = 0;
- while( $i < $len ) {
- $char = ord(substr($string, $i++, 1));
- if(valid_1byte($char)) { // continue
- continue;
- } else if(valid_2byte($char)) { // check 1 byte
- if(!valid_nextbyte(ord(substr($string, $i++, 1))))
- return false;
- } else if(valid_3byte($char)) { // check 2 bytes
- if(!valid_nextbyte(ord(substr($string, $i++, 1))))
- return false;
- if(!valid_nextbyte(ord(substr($string, $i++, 1))))
- return false;
- } else if(valid_4byte($char)) { // check 3 bytes
- if(!valid_nextbyte(ord(substr($string, $i++, 1))))
- return false;
- if(!valid_nextbyte(ord(substr($string, $i++, 1))))
- return false;
- if(!valid_nextbyte(ord(substr($string, $i++, 1))))
- return false;
- } // goto next char
- }
- return true; // done
- }
+
+function valid_1byte($char) {
+ if(!is_int($char)) return false;
+ return ($char & 0x80) == 0x00;
+}
+
+function valid_2byte($char) {
+ if(!is_int($char)) return false;
+ return ($char & 0xE0) == 0xC0;
+}
+
+function valid_3byte($char) {
+ if(!is_int($char)) return false;
+ return ($char & 0xF0) == 0xE0;
+}
+
+function valid_4byte($char) {
+ if(!is_int($char)) return false;
+ return ($char & 0xF8) == 0xF0;
+}
+
+function valid_nextbyte($char) {
+ if(!is_int($char)) return false;
+ return ($char & 0xC0) == 0x80;
+}
+
+function valid_utf8($string) {
+ $len = strlen($string);
+ $i = 0;
+ while( $i < $len ) {
+ $char = ord(substr($string, $i++, 1));
+ if(valid_1byte($char)) { // continue
+ continue;
+ } else if(valid_2byte($char)) { // check 1 byte
+ if(!valid_nextbyte(ord(substr($string, $i++, 1))))
+ return false;
+ } else if(valid_3byte($char)) { // check 2 bytes
+ if(!valid_nextbyte(ord(substr($string, $i++, 1))))
+ return false;
+ if(!valid_nextbyte(ord(substr($string, $i++, 1))))
+ return false;
+ } else if(valid_4byte($char)) { // check 3 bytes
+ if(!valid_nextbyte(ord(substr($string, $i++, 1))))
+ return false;
+ if(!valid_nextbyte(ord(substr($string, $i++, 1))))
+ return false;
+ if(!valid_nextbyte(ord(substr($string, $i++, 1))))
+ return false;
+ } // goto next char
+ }
+ return true; // done
+}