note 99925 modified in function.utf8-encode by danbrown
| From: | danbrown@php.net | Date: | Wed, 15 Sep 2010 15:27:45 +0000 |
| Subject: | note 99925 modified in function.utf8-encode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172579@lists.php.net to get a copy of this message | ||
Avoiding use of preg_match to detect if utf8_encode is needed:
<?php
$string = $string_input; // avoid being destructive
$string = preg_replace("#[\x09\x0A\x0D\x20-\x7E]#" ,"",$string); // ASCII
$string = preg_replace("#[\xC2-\xDF][\x80-\xBF]#" ,"",$string); //
non-overlong 2-byte
$string = preg_replace("#\xE0[\xA0-\xBF][\x80-\xBF]#" ,"",$string); //
excluding overlongs
$string = preg_replace("#[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}#","",$string);
// straight 3-byte
$string = preg_replace("#\xED[\x80-\x9F][\x80-\xBF]#" ,"",$string); //
excluding surrogates
$string = preg_replace("#\xF0[\x90-\xBF][\x80-\xBF]{2}#","",$string); //
planes 1-3
$string = preg_replace("#[\xF1-\xF3][\x80-\xBF]{3}#" ,"",$string); //
planes 4-15
$string = preg_replace("#\xF4[\x80-\x8F][\x80-\xBF]{2}#","",$string); //
plane 16
$rc = ($string == ""?true:false);
?>
--was--
Avoiding use of preg_match to detect if utf8_encode is needed:
$string = $string_input; // avoid being destructive
$string = preg_replace("#[\x09\x0A\x0D\x20-\x7E]#" ,"",$string); // ASCII
$string = preg_replace("#[\xC2-\xDF][\x80-\xBF]#" ,"",$string); //
non-overlong 2-byte
$string = preg_replace("#\xE0[\xA0-\xBF][\x80-\xBF]#" ,"",$string); //
excluding overlongs
$string = preg_replace("#[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}#","",$string);
// straight 3-byte
$string = preg_replace("#\xED[\x80-\x9F][\x80-\xBF]#" ,"",$string); //
excluding surrogates
$string = preg_replace("#\xF0[\x90-\xBF][\x80-\xBF]{2}#","",$string); //
planes 1-3
$string = preg_replace("#[\xF1-\xF3][\x80-\xBF]{3}#" ,"",$string); //
planes 4-15
$string = preg_replace("#\xF4[\x80-\x8F][\x80-\xBF]{2}#","",$string); //
plane 16
$rc = ($string == ""?true:false);
http://php.net/manual/en/function.utf8-encode.php