note 89286 added to function.mb-check-encoding

From: Date: Mon, 02 Mar 2009 02:52:39 +0000
Subject: note 89286 added to function.mb-check-encoding
Groups: php.notes 
Request: Send a blank email to php-notes+get-151307@lists.php.net to get a copy of this message
This function does not check for bad byte sequence(s), it only checks if the byte stream is valid. If you want to verify a encoded string is valid, (IE: does not contain any bad byte sequences do the following... <?php /* check a strings encoded value */ function checkEncoding ( $string, $string_encoding ) { $fs = $string_encoding == 'UTF-8' ? 'UTF-32' : $string_encoding; $ts = $string_encoding == 'UTF-32' ? 'UTF-8' : $string_encoding; return $string === mb_convert_encoding ( mb_convert_encoding ( $string, $fs, $ts ), $ts, $fs ); } /* test 1 variables */ $string = "\x00\x81"; $encoding = "Shift_JIS"; /* test 1 mb_check_encoding (test for bad byte stream) */ if ( true === mb_check_encoding ( $string, $encoding ) ) { echo 'valid (' . $encoding . ') encoded byte stream!<br />'; } else { echo 'invalid (' . $encoding . ') encoded byte stream!<br />'; } /* test 1 checkEncoding (test for bad byte sequence(s)) */ if ( true === checkEncoding ( $string, $encoding ) ) { echo 'valid (' . $encoding . ') encoded byte sequence!<br />'; } else { echo 'invalid (' . $encoding . ') encoded byte sequence!<br />'; } /* test 2 */ /* test 2 variables */ $string = "\x00\xE3"; $encoding = "UTF-8"; /* test 2 mb_check_encoding (test for bad byte stream) */ if ( true === mb_check_encoding ( $string, $encoding ) ) { echo 'valid (' . $encoding . ') encoded byte stream!<br />'; } else { echo 'invalid (' . $encoding . ') encoded byte stream!<br />'; } /* test 2 checkEncoding (test for bad byte sequence(s)) */ if ( true === checkEncoding ( $string, $encoding ) ) { echo 'valid (' . $encoding . ') encoded byte sequence!<br />'; } else { echo 'invalid (' . $encoding . ') encoded byte sequence!<br />'; } ?> ---- Server IP: 209.41.74.194 Probable Submitter: 24.60.7.123 ---- Manual Page -- http://www.php.net/manual/en/function.mb-check-encoding.php Edit -- https://master.php.net/note/edit/89286 Del: integrated -- https://master.php.net/note/delete/89286/integrated Del: useless -- https://master.php.net/note/delete/89286/useless Del: bad code -- https://master.php.net/note/delete/89286/bad+code Del: spam -- https://master.php.net/note/delete/89286/spam Del: non-english -- https://master.php.net/note/delete/89286/non-english Del: in docs -- https://master.php.net/note/delete/89286/in+docs Del: other reasons-- https://master.php.net/note/delete/89286 Reject -- https://master.php.net/note/reject/89286 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#151307) next »