Passed
Push — master ( a95f40...077740 )
by Domenico
02:21
created

Utils::unicode2chr()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Matecat\SubFiltering\Utils;
4
5
class Utils {
6
7
    /**
8
     * @param string $needle
9
     * @param string $haystack
10
     *
11
     * @return bool
12
     */
13
    public static function contains( $needle, $haystack ) {
14
        return strpos( $haystack, $needle ) !== false;
15
    }
16
17
    /**
18
     * Get the char code from a multibyte 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
            case 2:
32
                return ( ord( $mb_char[ 0 ] ) - 0xC0 ) * 0x40 +
33
                        ord( $mb_char[ 1 ] ) - 0x80;
34
            case 3:
35
                return ( ord( $mb_char[ 0 ] ) - 0xE0 ) * 0x1000 +
36
                        ( ord( $mb_char[ 1 ] ) - 0x80 ) * 0x40 +
37
                        ord( $mb_char[ 2 ] ) - 0x80;
38
            case 4:
39
                return ( ord( $mb_char[ 0 ] ) - 0xF0 ) * 0x40000 +
40
                        ( ord( $mb_char[ 1 ] ) - 0x80 ) * 0x1000 +
41
                        ( ord( $mb_char[ 2 ] ) - 0x80 ) * 0x40 +
42
                        ord( $mb_char[ 3 ] ) - 0x80;
43
        }
44
45
        return 20; //as default return a space ( should never happen )
46
    }
47
48
    /**
49
     * @param $str
50
     *
51
     * @return string
52
     */
53
    public static function htmlentitiesFromUnicode( $str ) {
54
        return "&#" . self::fastUnicode2ord( $str[ 1 ] ) . ";";
55
    }
56
57
    /**
58
     * multibyte string manipulation functions
59
     * source : http://stackoverflow.com/questions/9361303/can-i-get-the-unicode-value-of-a-character-or-vise-versa-with-php
60
     * original source : PHPExcel libary (http://phpexcel.codeplex.com/)
61
     * get the char from unicode code
62
     *
63
     * @param $o
64
     *
65
     * @return string
66
     */
67
    public static function unicode2chr( $o ) {
68
        if ( function_exists( 'mb_convert_encoding' ) ) {
69
            return mb_convert_encoding( '&#' . intval( $o ) . ';', 'UTF-8', 'HTML-ENTITIES' );
0 ignored issues
show
Bug Best Practice introduced by
The expression return mb_convert_encodi...TF-8', 'HTML-ENTITIES') also could return the type array which is incompatible with the documented return type string.
Loading history...
70
        }
71
72
        return chr( intval( $o ) );
73
    }
74
}