3 * This file is part of PHPUnit.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
14 * @since Class available since Release 3.6.0
16 class PHPUnit_Util_String
19 * Converts a string to UTF-8 encoding.
21 * @param string $string
25 public static function convertToUtf8($string)
27 if (!self::isUtf8($string)) {
28 if (function_exists('mb_convert_encoding')) {
29 $string = mb_convert_encoding($string, 'UTF-8');
31 $string = utf8_encode($string);
39 * Checks a string for UTF-8 encoding.
41 * @param string $string
45 protected static function isUtf8($string)
47 $length = strlen($string);
49 for ($i = 0; $i < $length; $i++) {
50 if (ord($string[$i]) < 0x80) {
52 } elseif ((ord($string[$i]) & 0xE0) == 0xC0) {
54 } elseif ((ord($string[$i]) & 0xF0) == 0xE0) {
56 } elseif ((ord($string[$i]) & 0xF0) == 0xF0) {
62 for ($j = 0; $j < $n; $j++) {
63 if ((++$i == $length) || ((ord($string[$i]) & 0xC0) != 0x80)) {