Conditions | 8 |
Paths | 17 |
Total Lines | 43 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Tests | 9 |
CRAP Score | 12.096 |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
11853 | public static function strwidth( |
||
11854 | string $str, |
||
11855 | string $encoding = 'UTF-8', |
||
11856 | bool $clean_utf8 = false |
||
11857 | ): int { |
||
11858 | 2 | if ($str === '') { |
|
11859 | 2 | return 0; |
|
11860 | } |
||
11861 | |||
11862 | 2 | if ($encoding !== 'UTF-8' && $encoding !== 'CP850') { |
|
11863 | 2 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
11864 | } |
||
11865 | |||
11866 | 2 | if ($clean_utf8) { |
|
11867 | // iconv and mbstring are not tolerant to invalid encoding |
||
11868 | // further, their behaviour is inconsistent with that of PHP's substr |
||
11869 | 2 | $str = self::clean($str); |
|
11870 | } |
||
11871 | |||
11872 | // |
||
11873 | // fallback via mbstring |
||
11874 | // |
||
11875 | |||
11876 | 2 | if (self::$SUPPORT['mbstring'] === true) { |
|
11877 | 2 | if ($encoding === 'UTF-8') { |
|
11878 | 2 | return \mb_strwidth($str); |
|
11879 | } |
||
11880 | |||
11881 | return \mb_strwidth($str, $encoding); |
||
11882 | } |
||
11883 | |||
11884 | // |
||
11885 | // fallback via vanilla php |
||
11886 | // |
||
11887 | |||
11888 | if ($encoding !== 'UTF-8') { |
||
11889 | $str = self::encode('UTF-8', $str, false, $encoding); |
||
11890 | } |
||
11891 | |||
11892 | $wide = 0; |
||
11893 | $str = (string) \preg_replace('/[\x{1100}-\x{115F}\x{2329}\x{232A}\x{2E80}-\x{303E}\x{3040}-\x{A4CF}\x{AC00}-\x{D7A3}\x{F900}-\x{FAFF}\x{FE10}-\x{FE19}\x{FE30}-\x{FE6F}\x{FF00}-\x{FF60}\x{FFE0}-\x{FFE6}\x{20000}-\x{2FFFD}\x{30000}-\x{3FFFD}]/u', '', $str, -1, $wide); |
||
11894 | |||
11895 | return ($wide << 1) + (int) self::strlen($str); |
||
11896 | } |
||
14700 |