diff --git a/src/voku/helper/UTF8.php b/src/voku/helper/UTF8.php index 78119a49..96c7cb9d 100644 --- a/src/voku/helper/UTF8.php +++ b/src/voku/helper/UTF8.php @@ -1280,6 +1280,16 @@ public static function encode($encoding, $str, $force = true) return self::to_iso8859($str); } + if ( + $encoding !== 'UTF-8' + && + $encoding !== 'WINDOWS-1252' + && + self::$support['mbstring'] === false + ) { + trigger_error('UTF8::encode() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING); + } + $strEncoded = \mb_convert_encoding( $str, $encoding, @@ -4727,10 +4737,14 @@ public static function stristr($haystack, $needle, $before_needle = false, $enco } if ( - $encoding !== 'UTF-8' // INFO: use "mb_"-function (with polyfill) also if we need another encoding - || - self::$support['mbstring'] === true + $encoding !== 'UTF-8' + && + self::$support['mbstring'] === false ) { + trigger_error('UTF8::stristr() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING); + } + + if (self::$support['mbstring'] === true) { return \mb_stristr($haystack, $needle, $before_needle, $encoding); } @@ -4792,10 +4806,14 @@ public static function strlen($str, $encoding = 'UTF-8', $cleanUtf8 = false) } if ( - $encoding !== 'UTF-8' // INFO: use "mb_"-function (with polyfill) also if we need another encoding - || - self::$support['mbstring'] === true + $encoding !== 'UTF-8' + && + self::$support['mbstring'] === false ) { + trigger_error('UTF8::strlen() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING); + } + + if (self::$support['mbstring'] === true) { return \mb_strlen($str, $encoding); } @@ -4813,6 +4831,7 @@ public static function strlen($str, $encoding = 'UTF-8', $cleanUtf8 = false) } } + // fallback via vanilla php preg_match_all('/./us', $str, $parts); $returnTmp = count($parts[0]); if ($returnTmp !== 0) { @@ -4980,14 +4999,22 @@ public static function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF- } if ( - $encoding !== 'UTF-8' // INFO: use "mb_"-function (with polyfill) also if we need another encoding - || - self::$support['mbstring'] === true + $encoding !== 'UTF-8' + && + self::$support['mbstring'] === false ) { + trigger_error('UTF8::strpos() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING); + } + + if (self::$support['mbstring'] === true) { return \mb_strpos($haystack, $needle, $offset, $encoding); } - if (self::$support['iconv'] === true) { + if ( + $offset >= 0 // iconv_strpos() can't handle negative offset + && + self::$support['iconv'] === true + ) { // ignore invalid negative offset to keep compatibility // with php < 5.5.35, < 5.6.21, < 7.0.6 return \iconv_strpos($haystack, $needle, $offset > 0 ? $offset : 0, $encoding); @@ -5013,7 +5040,13 @@ public static function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF- return false; } - return $offset + self::strlen(substr($haystack, 0, $pos)); + $returnTmp = $offset + self::strlen(substr($haystack, 0, $pos)); + if ($returnTmp !== false) { + return $returnTmp; + } + + // fallback to "mb_"-function via polyfill + return \mb_strpos($haystack, $needle, $offset); } /** @@ -5052,6 +5085,7 @@ public static function strrchr($haystack, $needle, $before_needle = false, $enco $haystack = self::clean($haystack); } + // fallback to "mb_"-function via polyfill return \mb_strrchr($haystack, $needle, $before_needle, $encoding); } @@ -5167,10 +5201,14 @@ public static function strripos($haystack, $needle, $offset = 0, $encoding = 'UT } if ( - $encoding !== 'UTF-8' // INFO: use "mb_"-function (with polyfill) also if we need another encoding - || - self::$support['mbstring'] === true + $encoding !== 'UTF-8' + && + self::$support['mbstring'] === false ) { + trigger_error('UTF8::strripos() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING); + } + + if (self::$support['mbstring'] === true) { return \mb_strripos($haystack, $needle, $offset, $encoding); } @@ -5181,6 +5219,8 @@ public static function strripos($haystack, $needle, $offset = 0, $encoding = 'UT } } + // fallback via vanilla php + return self::strrpos(self::strtolower($haystack, $encoding), self::strtolower($needle, $encoding), $offset, $encoding, $cleanUtf8); } @@ -5242,11 +5282,18 @@ public static function strrpos($haystack, $needle, $offset = null, $encoding = ' } if ( - $encoding !== 'UTF-8' // INFO: use "mb_"-function (with polyfill) also if we need another encoding - || - self::$support['mbstring'] === true + $encoding !== 'UTF-8' + && + self::$support['mbstring'] === false ) { - return \mb_strrpos($haystack, $needle, $offset, $encoding); + trigger_error('UTF8::strrpos() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING); + } + + if (self::$support['mbstring'] === true) { + $returnTmp = \mb_strrpos($haystack, $needle, $offset, $encoding); + if ($returnTmp !== false) { + return $returnTmp; + } } if (self::$support['intl'] === true) { @@ -5341,15 +5388,25 @@ public static function strstr($haystack, $needle, $before_needle = false, $encod } if ( - $encoding !== 'UTF-8' // INFO: use "mb_"-function (with polyfill) also if we need another encoding - || - self::$support['mbstring'] === true + $encoding !== 'UTF-8' + && + self::$support['mbstring'] === false ) { - return \mb_strstr($haystack, $needle, $before_needle, $encoding); + trigger_error('UTF8::strstr() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING); + } + + if (self::$support['mbstring'] === true) { + $returnTmp = \mb_strstr($haystack, $needle, $before_needle, $encoding); + if ($returnTmp !== false) { + return $returnTmp; + } } if (self::$support['intl'] === true) { - return \grapheme_strstr($haystack, $needle, $before_needle); + $returnTmp = \grapheme_strstr($haystack, $needle, $before_needle); + if ($returnTmp !== false) { + return $returnTmp; + } } preg_match('/^(.*?)' . preg_quote($needle, '/') . '/us', $haystack, $match); @@ -5551,6 +5608,7 @@ public static function strwidth($str, $encoding = 'UTF-8', $cleanUtf8 = false) $str = self::clean($str); } + // fallback to "mb_"-function via polyfill return \mb_strwidth($str, $encoding); } @@ -5613,15 +5671,23 @@ public static function substr($str, $start = 0, $length = null, $encoding = 'UTF } if ( - $encoding !== 'UTF-8' // INFO: use "mb_"-function (with polyfill) also if we need another encoding - || - self::$support['mbstring'] === true + $encoding !== 'UTF-8' + && + self::$support['mbstring'] === false ) { + trigger_error('UTF8::substr() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING); + } + + if (self::$support['mbstring'] === true) { return \mb_substr($str, $start, $length, $encoding); } - if (self::$support['iconv'] === true) { - return \iconv_substr($str, $start, $length, $encoding); + if ( + $length >= 0 // "iconv_substr()" can't handle negative length + && + self::$support['iconv'] === true + ) { + return \iconv_substr($str, $start, $length); } if (self::$support['intl'] === true) { @@ -5714,10 +5780,14 @@ public static function substr_count($haystack, $needle, $offset = 0, $length = n } if ( - $encoding !== 'UTF-8' // INFO: use "mb_"-function (with polyfill) also if we need another encoding - || - self::$support['mbstring'] === true + $encoding !== 'UTF-8' + && + self::$support['mbstring'] === false ) { + trigger_error('UTF8::substr_count() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING); + } + + if (self::$support['mbstring'] === true) { return \mb_substr_count($haystack, $needle, $encoding); } @@ -5736,6 +5806,7 @@ public static function substr_count($haystack, $needle, $offset = 0, $length = n */ public static function substr_ileft($haystack, $needle) { + // init $haystack = (string)$haystack; $needle = (string)$needle; @@ -5764,6 +5835,7 @@ public static function substr_ileft($haystack, $needle) */ public static function substr_iright($haystack, $needle) { + // init $haystack = (string)$haystack; $needle = (string)$needle; @@ -5792,6 +5864,7 @@ public static function substr_iright($haystack, $needle) */ public static function substr_left($haystack, $needle) { + // init $haystack = (string)$haystack; $needle = (string)$needle; @@ -5815,12 +5888,24 @@ public static function substr_left($haystack, $needle) * * source: https://gist.github.com/stemar/8287074 * - * @param string|string[] $str

The input string or an array of stings.

- * @param string|string[] $replacement

The replacement string or an array of stings.

- * @param int|int[] $start - * @param int|int[]|void $length [optional] - * - * @return string|string[] + * @param string|string[] $str

The input string or an array of stings.

+ * @param string|string[] $replacement

The replacement string or an array of stings.

+ * @param int|int[] $start

+ * If start is positive, the replacing will begin at the start'th offset + * into string. + *

+ * If start is negative, the replacing will begin at the start'th character + * from the end of string. + *

+ * @param int|int[]|void $length [optional]

If given and is positive, it represents the length of the + * portion of string which is to be replaced. If it is negative, it + * represents the number of characters from the end of string at which to + * stop replacing. If it is not given, then it will default to strlen( + * string ); i.e. end the replacing at the end of string. Of course, if + * length is zero then this function will have the effect of inserting + * replacement into string at the given start offset.

+ * + * @return string|string[]

The result string is returned. If string is an array then array is returned.

*/ public static function substr_replace($str, $replacement, $start, $length = null) { @@ -5864,7 +5949,9 @@ public static function substr_replace($str, $replacement, $start, $length = null // Recursive call return array_map(array(__CLASS__, 'substr_replace'), $str, $replacement, $start, $length); + } else { + if (is_array($replacement)) { if (count($replacement) > 0) { $replacement = $replacement[0]; @@ -5874,11 +5961,19 @@ public static function substr_replace($str, $replacement, $start, $length = null } } - preg_match_all('/./us', (string)$str, $smatches); - preg_match_all('/./us', (string)$replacement, $rmatches); + // init + $str = (string)$str; + $replacement = (string)$replacement; + + if (!isset($str[0])) { + return $replacement; + } + + preg_match_all('/./us', $str, $smatches); + preg_match_all('/./us', $replacement, $rmatches); if ($length === null) { - $length = (int)\mb_strlen($str); + $length = (int)self::strlen($str); } array_splice($smatches[0], $start, $length, $rmatches[0]); @@ -6308,13 +6403,7 @@ function ($match) { ); // decode UTF-8 codepoints - $buf = preg_replace_callback( - '/&#\d{2,6};/', - function ($match) { - return \mb_convert_encoding($match[0], 'UTF-8', 'HTML-ENTITIES'); - }, - $buf - ); + $buf = self::html_entity_decode($buf, ENT_QUOTES); return $buf; } diff --git a/tests/Utf8GlobalTest.php b/tests/Utf8GlobalTest.php index 71b1100a..7cf3b454 100644 --- a/tests/Utf8GlobalTest.php +++ b/tests/Utf8GlobalTest.php @@ -515,8 +515,10 @@ public function testEncode() 'Berbée' => 'Berb?e', ); - foreach ($tests as $before => $after) { - self::assertSame($after, UTF8::encode('CP367', $before), 'tested: ' . $before); // CP367 + if (UTF8::mbstring_loaded() === true) { // only with "mbstring" + foreach ($tests as $before => $after) { + self::assertSame($after, UTF8::encode('CP367', $before), 'tested: ' . $before); // CP367 + } } $tests = array( @@ -1112,7 +1114,6 @@ public function testHtmlEntityDecodeWithEntQuotes() 'who&#039;s online' => 'who\'s online', 'who's online-' => 'who\'s online-', 'Who's Online' => 'Who\'s Online', - 'Who&#039;s Online 中' => 'Who\'s Online ?', 'Who&amp;#039;s Online' => 'Who\'s Online', "Who\'s Online:" => 'Who\\\'s Online:', ); @@ -1120,6 +1121,12 @@ public function testHtmlEntityDecodeWithEntQuotes() foreach ($testArray as $before => $after) { self::assertSame($after, UTF8::html_entity_decode($before, ENT_QUOTES, 'ISO'), 'error by ' . $before); // 'ISO-8859-1' } + + if (UTF8::mbstring_loaded() === true) { // only with "mbstring" + self::assertSame('Who\'s Online ?', UTF8::html_entity_decode('Who&#039;s Online 中', ENT_QUOTES, 'ISO')); + } else { + self::assertSame('Who\'s Online ', UTF8::html_entity_decode('Who&#039;s Online 中', ENT_QUOTES, 'ISO')); + } } public function testHtmlEntityDecodeWithHtml5() @@ -2492,10 +2499,13 @@ public function testStrlen() self::assertSame(74, strlen($string)); self::assertSame(74, UTF8::strlen($string, '8bit')); - self::assertSame(71, UTF8::strlen($string)); - self::assertSame(71, UTF8::strlen($string, 'UTF-8', false)); self::assertSame(67, UTF8::strlen($string, 'UTF-8', true)); + if (UTF8::mbstring_loaded() === true) { // only with "mbstring" + self::assertSame(71, UTF8::strlen($string)); + self::assertSame(71, UTF8::strlen($string, 'UTF-8', false)); + } + $string_test1 = strip_tags($string); $string_test2 = UTF8::strip_tags($string); @@ -2694,31 +2704,33 @@ public function testStrpos() // --- invalid UTF-8 - self::assertSame(15, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白')); + if (UTF8::mbstring_loaded() === true) { // only with "mbstring" + self::assertSame(15, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白')); - if (Bootup::is_php('7.1') === false) { - self::assertSame(false, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', -8)); - } else { - self::assertSame(20, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', -8)); - } + if (Bootup::is_php('7.1') === false) { + self::assertSame(false, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', -8)); + } else { + self::assertSame(20, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', -8)); + } - self::assertSame(false, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', -4)); - self::assertSame(false, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', -1)); - self::assertSame(15, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', 0)); - self::assertSame(15, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', 4)); - self::assertSame(15, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', 8)); - self::assertSame(14, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', 0, 'UTF-8', true)); - self::assertSame(15, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', 0, 'UTF-8', false)); - self::assertSame(26, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', 0, 'ISO', true)); - self::assertSame(27, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', 0, 'ISO', false)); + self::assertSame(false, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', -4)); + self::assertSame(false, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', -1)); + self::assertSame(15, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', 0)); + self::assertSame(15, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', 4)); + self::assertSame(15, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', 8)); + self::assertSame(14, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', 0, 'UTF-8', true)); + self::assertSame(15, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', 0, 'UTF-8', false)); + self::assertSame(26, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', 0, 'ISO', true)); + self::assertSame(27, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', '白', 0, 'ISO', false)); - // ISO + // ISO - self::assertSame(17, strpos('der Straße nach Paris', 'Paris', 0)); // not correct - self::assertSame(17, UTF8::strpos('der Straße nach Paris', 'Paris', 0, 'ISO')); // not correct + self::assertSame(17, strpos('der Straße nach Paris', 'Paris', 0)); // not correct + self::assertSame(17, UTF8::strpos('der Straße nach Paris', 'Paris', 0, 'ISO')); // not correct - self::assertSame(3, strpos('한국어', '국', 0)); // not correct - self::assertSame(3, UTF8::strpos('한국어', '국', 0, 'ISO')); // not correct + self::assertSame(3, strpos('한국어', '국', 0)); // not correct + self::assertSame(3, UTF8::strpos('한국어', '국', 0, 'ISO')); // not correct + } } } @@ -2749,11 +2761,13 @@ public function testStrrchr() // --- ISO - self::assertSame('κόσμε-äöü', UTF8::strrchr('κόσμεκόσμε-äöü', 'κόσμε', false, 'ISO')); - self::assertSame(false, UTF8::strrchr('Aκόσμεκόσμε-äöü', 'aκόσμε', false, 'ISO')); + if (UTF8::mbstring_loaded() === true) { // only with "mbstring" + self::assertSame('κόσμε-äöü', UTF8::strrchr('κόσμεκόσμε-äöü', 'κόσμε', false, 'ISO')); + self::assertSame(false, UTF8::strrchr('Aκόσμεκόσμε-äöü', 'aκόσμε', false, 'ISO')); - self::assertSame('κόσμε', UTF8::strrchr('κόσμεκόσμε-äöü', 'κόσμε', true, 'ISO')); - self::assertSame(false, UTF8::strrchr('Aκόσμεκόσμε-äöü', 'aκόσμε', true, 'ISO')); + self::assertSame('κόσμε', UTF8::strrchr('κόσμεκόσμε-äöü', 'κόσμε', true, 'ISO')); + self::assertSame(false, UTF8::strrchr('Aκόσμεκόσμε-äöü', 'aκόσμε', true, 'ISO')); + } } public function testStrrev() @@ -2799,12 +2813,13 @@ public function testStrrichr() // --- ISO - self::assertSame('Aκόσμεκόσμε-äöü', UTF8::strrichr('Aκόσμεκόσμε-äöü', 'aκόσμε', false, 'ISO')); - self::assertSame('ü-abc', UTF8::strrichr('äöü-abc', 'ü', false, 'ISO')); - - self::assertSame('', UTF8::strrichr('Aκόσμεκόσμε-äöü', 'aκόσμε', true, 'ISO')); - self::assertSame('äö', UTF8::strrichr('äöü-abc', 'ü', true, 'ISO')); + if (UTF8::mbstring_loaded() === true) { // only with "mbstring" + self::assertSame('Aκόσμεκόσμε-äöü', UTF8::strrichr('Aκόσμεκόσμε-äöü', 'aκόσμε', false, 'ISO')); + self::assertSame('ü-abc', UTF8::strrichr('äöü-abc', 'ü', false, 'ISO')); + self::assertSame('', UTF8::strrichr('Aκόσμεκόσμε-äöü', 'aκόσμε', true, 'ISO')); + self::assertSame('äö', UTF8::strrichr('äöü-abc', 'ü', true, 'ISO')); + } } public function testStrrpos() @@ -2816,11 +2831,14 @@ public function testStrrpos() self::assertSame(1, UTF8::strrpos('한국어', '국', 0, '8bit', false)); self::assertSame(1, UTF8::strrpos('한국어', '국', 0, 'ISO', false)); self::assertSame(1, UTF8::strrpos('한국어', '국', 0, '', true)); - } else { - self::assertSame(3, UTF8::strrpos('한국어', '국', 0, '8bit', false)); - self::assertSame(3, UTF8::strrpos('한국어', '국', 0, 'ISO', false)); - self::assertSame(false, UTF8::strrpos('한국어', '국', 0, '', true)); + + if (UTF8::mbstring_loaded() === true) { // only with "mbstring" + self::assertSame(3, UTF8::strrpos('한국어', '국', 0, '8bit', false)); + self::assertSame(3, UTF8::strrpos('한국어', '국', 0, 'ISO', false)); + } + + self::assertSame(1, UTF8::strrpos('한국어', '국', 0, '', true)); } self::assertSame(1, UTF8::strrpos('한국어', '국', 0, 'UTF-8', false)); @@ -2855,10 +2873,11 @@ public function testStrtocasefold() // invalid utf-8 - if (Bootup::is_php('5.4')) { - // invalid UTF-8 + PHP 5.3 = 20 => error - self::assertSame('iñtërnâtiôn?àlizætiøn', UTF8::strtocasefold("Iñtërnâtiôn\xE9àlizætiøn")); - self::assertSame('iñtërnâtiôn?àlizætiøn', UTF8::strtocasefold("Iñtërnâtiôn\xE9àlizætiøn", true)); + if (UTF8::mbstring_loaded() === true) { // only with "mbstring" + if (Bootup::is_php('5.4')) { // invalid UTF-8 + PHP 5.3 = 20 => error + self::assertSame('iñtërnâtiôn?àlizætiøn', UTF8::strtocasefold("Iñtërnâtiôn\xE9àlizætiøn")); + self::assertSame('iñtërnâtiôn?àlizætiøn', UTF8::strtocasefold("Iñtërnâtiôn\xE9àlizætiøn", true)); + } } self::assertSame('iñtërnâtiônàlizætiøn', UTF8::strtocasefold("Iñtërnâtiôn\xE9àlizætiøn", true, true)); @@ -2970,20 +2989,26 @@ public function testStrwidth() // test + Invalid Chars - if (Bootup::is_php('5.4')) { - // invalid UTF-8 + PHP 5.3 = 20 => error - self::assertSame(21, UTF8::strwidth("Iñtërnâtiôn\xE9àlizætiøn", 'UTF8', false)); + if (UTF8::mbstring_loaded() === true) { // only with "mbstring" + if (Bootup::is_php('5.4')) { // invalid UTF-8 + PHP 5.3 = 20 => error + self::assertSame(21, UTF8::strwidth("Iñtërnâtiôn\xE9àlizætiøn", 'UTF8', false)); + } } self::assertSame(20, UTF8::strwidth("Iñtërnâtiôn\xE9àlizætiøn", 'UTF8', true)); - self::assertSame(20, UTF8::strlen("Iñtërnâtiôn\xE9àlizætiøn", 'UTF8', false)); + if (UTF8::mbstring_loaded() === true) { // only with "mbstring" + self::assertSame(20, UTF8::strlen("Iñtërnâtiôn\xE9àlizætiøn", 'UTF8', false)); + } + self::assertSame(20, UTF8::strlen("Iñtërnâtiôn\xE9àlizætiøn", 'UTF8', true)); // ISO - self::assertSame(28, UTF8::strlen("Iñtërnâtiôn\xE9àlizætiøn", 'ISO', false)); - self::assertSame(27, UTF8::strlen("Iñtërnâtiôn\xE9àlizætiøn", 'ISO', true)); + if (UTF8::mbstring_loaded() === true) { // only with "mbstring" + self::assertSame(28, UTF8::strlen("Iñtërnâtiôn\xE9àlizætiøn", 'ISO', false)); + self::assertSame(27, UTF8::strlen("Iñtërnâtiôn\xE9àlizætiøn", 'ISO', true)); + } } public function testSubstr() @@ -3103,8 +3128,10 @@ public function testSubstrCount() // ISO - self::assertSame(0, UTF8::substr_count('中文空白', '文空', 1, 2, 'ISO')); - self::assertSame(1, UTF8::substr_count('abcde', 'bc', 1, 2, 'ISO')); + if (UTF8::mbstring_loaded() === true) { // only with "mbstring" + self::assertSame(0, UTF8::substr_count('中文空白', '文空', 1, 2, 'ISO')); + self::assertSame(1, UTF8::substr_count('abcde', 'bc', 1, 2, 'ISO')); + } } public function testSubstrILeft() diff --git a/tests/Utf8StristrTest.php b/tests/Utf8StristrTest.php index c3de39c4..efda4896 100644 --- a/tests/Utf8StristrTest.php +++ b/tests/Utf8StristrTest.php @@ -82,8 +82,10 @@ public function test_encoding() // UTF-8 self::assertSame("n\nâtiônàlizætiøn", u::stristr($str, $search, 0, 'UTF-8', false)); - // UTF-7 - self::assertSame("n\n??ti??n??liz??ti??n", u::stristr($str, $search, 0, 'UTF-7', false)); + if (u::mbstring_loaded() === true) { // only with "mbstring" + // UTF-7 + self::assertSame("n\n??ti??n??liz??ti??n", u::stristr($str, $search, 0, 'UTF-7', false)); + } } public function test_clean_utf8() diff --git a/tests/Utf8StrlenTest.php b/tests/Utf8StrlenTest.php index 6c19c168..4ab9df43 100644 --- a/tests/Utf8StrlenTest.php +++ b/tests/Utf8StrlenTest.php @@ -15,8 +15,10 @@ public function test_utf8() public function test_utf8_invalid() { - $str = "Iñtërnâtiôn\xE9àlizætiøn"; - self::assertSame(20, u::strlen($str)); + if (u::mbstring_loaded() === true) { // only with "mbstring" + $str = "Iñtërnâtiôn\xE9àlizætiøn"; + self::assertSame(20, u::strlen($str)); + } } public function test_ascii()