note 51744 added to function.utf8-encode
| From: | JFSebastian at osu1 dot php dot net | Date: | Sat, 09 Apr 2005 10:54:55 +0000 |
| Subject: | note 51744 added to function.utf8-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-87756@lists.php.net to get a copy of this message | ||
The following Perl regular expression tests if a string is well-formed Unicode UTF-8 (Broken up
after each | since long lines are not permitted here. Please join as a single line, no spaces,
before use.):
^([\x00-\x7f]|
[\xc2-\xdf][\x80-\xbf]|
\xe0[\xa0-\xbf][\x80-\xbf]|
[\xe1-\xec][\x80-\xbf]{2}|
\xed[\x80-\x9f][\x80-\xbf]|
[\xee-\xef][\x80-\xbf]{2}|
f0[\x90-\xbf][\x80-\xbf]{2}|
[\xf1-\xf3][\x80-\xbf]{3}|
\xf4[\x80-\x8f][\x80-\xbf]{2})*$
NOTE: This strictly follows the Unicode standard 4.0, as described in chapter 3.9, table 3-6,
"Well-formed UTF-8 byte sequences" ( http://www.unicode.org/versions/Unicode4.0.0/ch03.pdf#G31703
).
ISO-10646, a super-set of Unicode, uses UTF-8 (there called "UCS", see http://www.unicode.org/faq/utf_bom.html#1 ) in
a relaxed variant that supports a 31-bit space encoded into up to six bytes instead of
Unicode's 21 bits in up to four bytes. To check for ISO-10646 UTF-8, use the following Perl
regular expression (again, broken up, see above):
^([\x00-\x7f]|
[\xc0-\xdf][\x80-\xbf]|
[\xe0-\xef][\x80-\xbf]{2}|
[\xf0-\xf7][\x80-\xbf]{3}|
[\xf8-\xfb][\x80-\xbf]{4}|
[\xfc-\xfd][\x80-\xbf]{5})*$
The following function may be used with above expressions for a quick UTF-8 test, e.g. to
distinguish ISO-8859-1-data from UTF-8-data if submitted from a <form
accept-charset="utf-8,iso-8859-1" method=..>.
function is_utf8($string) {
return (preg_match('/[insert regular expression here]/', $string) === 1);
}
----
Manual Page -- http://www.php.net/manual/en/function.utf8-encode.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51744
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51744&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51744&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51744&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51744&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51744&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51744&report=yes
Search -- http://master.php.net/manage/user-notes.php