1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Matecat\SubFiltering\Utils; |
4
|
|
|
|
5
|
|
|
class CatUtils { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @param string $needle |
9
|
|
|
* @param string $haystack |
10
|
|
|
* |
11
|
|
|
* @return bool |
12
|
|
|
*/ |
13
|
|
|
public static function contains( $needle, $haystack ) { |
14
|
|
|
return mb_strpos( $haystack, $needle ) !== false; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Get the char code from a multi byte char |
19
|
|
|
* |
20
|
|
|
* 2/3 times faster than the old implementation |
21
|
|
|
* |
22
|
|
|
* @param $mb_char string Unicode Multibyte Char String |
23
|
|
|
* |
24
|
|
|
* @return int |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
public static function fastUnicode2ord( $mb_char ) { |
28
|
|
|
switch ( strlen( $mb_char ) ) { |
29
|
|
|
case 1: |
30
|
|
|
return ord( $mb_char ); |
31
|
|
|
break; |
|
|
|
|
32
|
|
|
case 2: |
33
|
|
|
return ( ord( $mb_char[ 0 ] ) - 0xC0 ) * 0x40 + |
34
|
|
|
ord( $mb_char[ 1 ] ) - 0x80; |
35
|
|
|
break; |
36
|
|
|
case 3: |
37
|
|
|
return ( ord( $mb_char[ 0 ] ) - 0xE0 ) * 0x1000 + |
38
|
|
|
( ord( $mb_char[ 1 ] ) - 0x80 ) * 0x40 + |
39
|
|
|
ord( $mb_char[ 2 ] ) - 0x80; |
40
|
|
|
break; |
41
|
|
|
case 4: |
42
|
|
|
return ( ord( $mb_char[ 0 ] ) - 0xF0 ) * 0x40000 + |
43
|
|
|
( ord( $mb_char[ 1 ] ) - 0x80 ) * 0x1000 + |
44
|
|
|
( ord( $mb_char[ 2 ] ) - 0x80 ) * 0x40 + |
45
|
|
|
ord( $mb_char[ 3 ] ) - 0x80; |
46
|
|
|
break; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return 20; //as default return a space ( should never happen ) |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $str |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public static function htmlentitiesFromUnicode( $str ) { |
58
|
|
|
return "&#" . self::fastUnicode2ord( $str[ 1 ] ) . ";"; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* multibyte string manipulation functions |
63
|
|
|
* source : http://stackoverflow.com/questions/9361303/can-i-get-the-unicode-value-of-a-character-or-vise-versa-with-php |
64
|
|
|
* original source : PHPExcel libary (http://phpexcel.codeplex.com/) |
65
|
|
|
* get the char from unicode code |
66
|
|
|
* |
67
|
|
|
* @param $o |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public static function unicode2chr( $o ) { |
72
|
|
|
if ( function_exists( 'mb_convert_encoding' ) ) { |
73
|
|
|
return mb_convert_encoding( '&#' . intval( $o ) . ';', 'UTF-8', 'HTML-ENTITIES' ); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return chr( intval( $o ) ); |
77
|
|
|
} |
78
|
|
|
} |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.