ThreemaGateway_Helper_Emoji::replaceDigits()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Handles emoji stuff.
4
 *
5
 * @package ThreemaGateway
6
 * @author rugk
7
 * @copyright Copyright (c) 2016 rugk
8
 * @license MIT
9
 */
10
11
class ThreemaGateway_Helper_Emoji
12
{
13
    /**
14
     * Replaces unicode escape sequence with the correct UNICODE character.
15
     *
16
     * XenForo template helper: emojiparseunicode.
17
     * You need to pass it as surrogate pairs, e.g. \ud83d\udd11.
18
     *
19
     * @param  string $string
20
     * @return string
21
     */
22
    public static function parseUnicode($string)
23
    {
24
        // uses json_decode as a hackish way to encode unicode strings
25
        // https://stackoverflow.com/questions/6058394/unicode-character-in-php-string
26
27
        // RegEx: https://regex101.com/r/yS2zX8/3
28
        return preg_replace_callback('/(\\\\u([0-9a-fA-F]{4}))+/', function ($match) {
29
            return json_decode('"' . $match[0] . '"');
30
        }, $string);
31
    }
32
33
    /**
34
     * Replaces digits with their corresponding unicode characters
35
     * (surrogate pairs).
36
     *
37
     * XenForo template helper: emojireplacedigits.
38
     * Only replaces one digit numbers. "10" is therefore replaced by two unicode
39
     * characters.
40
     *
41
     * @param  string $string
42
     * @return string
43
     */
44
    public static function replaceDigits($string)
45
    {
46
        // add \u20e3 to every number
47
        // https://regex101.com/r/aQ3eA3/1
48
        return preg_replace('/(\d)/', '\1\\u20e3', $string);
49
    }
50
}
51