src/NativeEncoder.php
protected static function isUTF8($text)
    {
        /*
            Based on http://w3.org/International/questions/qa-forms-utf-8.html,
            but rewritten to avoid a dependency on PCRE.

            Also, the regular expression in the post above seems to limit
            valid inputs to printable characters (for compatibility reasons?).
            See also section 2.2 of http://www.w3.org/TR/xml11 and section 2.2
            of http://www.w3.org/TR/xml/ for a possible explanation.

            The code below does not suffer from such limitations.
        */
        $len = strlen($text);
        $res = true;
        for ($i = 0; $i < $len; $i++) {
            // U+0000 - U+007F
            $byte1 = ord($text[$i]);
            if ($byte1 >= 0 && $byte1 <= 0x7F) {
                continue;
            }

            // Look for 2nd byte
            if (++$i >= $len) {
                return false;
            }
            $byte2 = ord($text[$i]);

            // U+0080 - U-07FF
            if ($byte1 >= 0xC2 && $byte1 <= 0xDF) {
                if ($byte2 >= 0x80 && $byte2 <= 0xBF) {
                    continue;
                }
                return false;
            }

            // Look for 3nd byte
            if (++$i >= $len) {
                return false;
            }
            $byte3 = ord($text[$i]);

            // U+0800 - U+0FFF
            if ($byte1 == 0xE0) {
                if ($byte2 >= 0xA0 && $byte2 <= 0xBF &&
                    $byte3 >= 0x80 && $byte3 <= 0xBF) {
                    continue;
                }
                return false;
            }

            // U+1000 - U+CFFF & U+E000 - U+FFFF
            if ($byte1 >= 0xE1 && $byte1 <= 0xEF && $byte1 !== 0xED) {
                if ($byte2 >= 0x80 && $byte2 <= 0xBF &&
                    $byte3 >= 0x80 && $byte3 <= 0xBF) {
                    $codepoint = (($byte1 & 0x0F) << 12) + (($byte2 & 0x3F) << 6) + ($byte3 & 0x3F);
                    if (($codepoint >= 0xE000 && $codepoint <= 0xF8FF) ||   // Private range
                        ($codepoint >= 0xFDD0 && $codepoint <= 0xFDEF) ||   // Non-characters
                        $codepoint == 0xFFFE || $codepoint == 0xFFFF) {     // Non-characters
                        $res = null;
                    }
                    continue;
                }
                return false;
            }

            // U+D000 - U+D7FF
            if ($byte1 == 0xED) {
                if ($byte2 >= 0x80 && $byte2 <= 0x9F &&
                    $byte3 >= 0x80 && $byte3 <= 0xBF) {
                    continue;
                }
                return false;
            }

            // Look for 4nd byte
            if (++$i >= $len) {
                return false;
            }
            $byte4 = ord($text[$i]);

            // U+10000 - U+3FFFF
            if ($byte1 == 0xF0) {
                if ($byte2 >= 0x90 && $byte2 <= 0xBF &&
                    $byte3 >= 0x80 && $byte3 <= 0xBF &&
                    $byte4 >= 0x80 && $byte4 <= 0xBF) {
                    $codepoint = (($byte1 & 0x07) << 18) +
                                 (($byte2 & 0x3F) << 12) +
                                 (($byte3 & 0x3F) << 6) +
                                 ($byte4 & 0x3F);
                    //  Non-characters                                    Reserved range
                    if ($codepoint == 0x1FFFE || $codepoint == 0x1FFFF || $codepoint >= 0x2FFFE) {
                        $res = null;
                    }
                    continue;
                }
                return false;
            }

            // U+40000 - U+FFFFF
            if ($byte1 >= 0xF1 && $byte1 <= 0xF3) {
                if ($byte2 >= 0x80 && $byte2 <= 0xBF &&
                    $byte3 >= 0x80 && $byte3 <= 0xBF &&
                    $byte4 >= 0x80 && $byte4 <= 0xBF) {
                    $codepoint = (($byte1 & 0x07) << 18) +
                                 (($byte2 & 0x3F) << 12) +
                                 (($byte3 & 0x3F) << 6) +
                                 ($byte4 & 0x3F);
                    //  Reserved range           Non characters & private ranges
                    if ($codepoint < 0xE0000 || $codepoint >= 0xEFFFE) {
                        $res = null;
                    }
                    continue;
                }
                return false;
            }

            // U+100000 - U+10FFFF
            if ($byte1 == 0xF4) {
                if ($byte2 >= 0x80 && $byte2 <= 0x8F &&
                    $byte3 >= 0x80 && $byte3 <= 0xBF &&
                    $byte4 >= 0x80 && $byte4 <= 0xBF) {
                    // This part contains only non-characters & private ranges.
                    $res = null;
                    continue;
                }
                return false;
            }

            // Byte #1 contained an invalid value.
            return false;
        }

        // No decoding error detected, but the given input may contain
        // non-characters/reserved characters, depending on the value of $res.
        return $res;
    }
protected static function isUTF8($text)
    {
        /*
            Based on http://w3.org/International/questions/qa-forms-utf-8.html,
            but rewritten to avoid a dependency on PCRE.

            Also, the regular expression in the post above seems to limit
            valid inputs to printable characters (for compatibility reasons?).
            See also section 2.2 of http://www.w3.org/TR/xml11 and section 2.2
            of http://www.w3.org/TR/xml/ for a possible explanation.

            The code below does not suffer from such limitations.
        */
        $len = strlen($text);
        $res = true;
        for ($i = 0; $i < $len; $i++) {
            // U+0000 - U+007F
            $byte1 = ord($text[$i]);
            if ($byte1 >= 0 && $byte1 <= 0x7F) {
                continue;
            }

            // Look for 2nd byte
            if (++$i >= $len) {
                return false;
            }
            $byte2 = ord($text[$i]);

            // U+0080 - U-07FF
            if ($byte1 >= 0xC2 && $byte1 <= 0xDF) {
                if ($byte2 >= 0x80 && $byte2 <= 0xBF) {
                    continue;
                }
                return false;
            }

            // Look for 3nd byte
            if (++$i >= $len) {
                return false;
            }
            $byte3 = ord($text[$i]);

            // U+0800 - U+0FFF
            if ($byte1 == 0xE0) {
                if ($byte2 >= 0xA0 && $byte2 <= 0xBF &&
                    $byte3 >= 0x80 && $byte3 <= 0xBF) {
                    continue;
                }
                return false;
            }

            // U+1000 - U+CFFF & U+E000 - U+FFFF
            if ($byte1 >= 0xE1 && $byte1 <= 0xEF && $byte1 !== 0xED) {
                if ($byte2 >= 0x80 && $byte2 <= 0xBF &&
                    $byte3 >= 0x80 && $byte3 <= 0xBF) {
                    $codepoint = (($byte1 & 0x0F) << 12) + (($byte2 & 0x3F) << 6) + ($byte3 & 0x3F);
                    if (($codepoint >= 0xE000 && $codepoint <= 0xF8FF) ||   // Private range
                        ($codepoint >= 0xFDD0 && $codepoint <= 0xFDEF) ||   // Non-characters
                        $codepoint == 0xFFFE || $codepoint == 0xFFFF) {     // Non-characters
                        $res = null;
                    }
                    continue;
                }
                return false;
            }

            // U+D000 - U+D7FF
            if ($byte1 == 0xED) {
                if ($byte2 >= 0x80 && $byte2 <= 0x9F &&
                    $byte3 >= 0x80 && $byte3 <= 0xBF) {
                    continue;
                }
                return false;
            }

            // Look for 4nd byte
            if (++$i >= $len) {
                return false;
            }
            $byte4 = ord($text[$i]);

            // U+10000 - U+3FFFF
            if ($byte1 == 0xF0) {
                if ($byte2 >= 0x90 && $byte2 <= 0xBF &&
                    $byte3 >= 0x80 && $byte3 <= 0xBF &&
                    $byte4 >= 0x80 && $byte4 <= 0xBF) {
                    $codepoint = (($byte1 & 0x07) << 18) +
                                 (($byte2 & 0x3F) << 12) +
                                 (($byte3 & 0x3F) << 6) +
                                 ($byte4 & 0x3F);
                    //  Non-characters                                    Reserved range
                    if ($codepoint == 0x1FFFE || $codepoint == 0x1FFFF || $codepoint >= 0x2FFFE) {
                        $res = null;
                    }
                    continue;
                }
                return false;
            }

            // U+40000 - U+FFFFF
            if ($byte1 >= 0xF1 && $byte1 <= 0xF3) {
                if ($byte2 >= 0x80 && $byte2 <= 0xBF &&
                    $byte3 >= 0x80 && $byte3 <= 0xBF &&
                    $byte4 >= 0x80 && $byte4 <= 0xBF) {
                    $codepoint = (($byte1 & 0x07) << 18) +
                                 (($byte2 & 0x3F) << 12) +
                                 (($byte3 & 0x3F) << 6) +
                                 ($byte4 & 0x3F);
                    //  Reserved range           Non characters & private ranges
                    if ($codepoint < 0xE0000 || $codepoint >= 0xEFFFE) {
                        $res = null;
                    }
                    continue;
                }
                return false;
            }

            // U+100000 - U+10FFFF
            if ($byte1 == 0xF4) {
                if ($byte2 >= 0x80 && $byte2 <= 0x8F &&
                    $byte3 >= 0x80 && $byte3 <= 0xBF &&
                    $byte4 >= 0x80 && $byte4 <= 0xBF) {
                    // This part contains only non-characters & private ranges.
                    $res = null;
                    continue;
                }
                return false;
            }

            // Byte #1 contained an invalid value.
            return false;
        }

        // No decoding error detected, but the given input may contain
        // non-characters/reserved characters, depending on the value of $res.
        return $res;
    }
Determine if a string contains a valid sequence of UTF-8 encoded
Unicode codepoints.

\param string $text
Some text to test for UTF-8 validity.

\retval bool
\c true if the $text contains a valid UTF-8 sequence
which does not make use of non-characters/reserved characters,
\c null if it contains a valid UTF-8 containing
non-characters/reserved characters, or \c false otherwise.