1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace BahriCanli\LaravelEmoji; |
3
|
|
|
|
4
|
|
|
use App\Http\Controllers\Controller; |
5
|
|
|
require_once 'Emoji.php'; |
6
|
|
|
|
7
|
|
|
class LaravelEmoji extends Controller { |
8
|
|
|
|
9
|
|
|
static public function covertEmojiToName($data) { |
|
|
|
|
10
|
|
|
$data = emoji_docomo_to_unified($data); # DoCoMo devices |
11
|
|
|
$data = emoji_kddi_to_unified($data); # KDDI & Au devices |
12
|
|
|
$data = emoji_softbank_to_unified($data); # Softbank & (iPhone) Apple devices |
13
|
|
|
$data = emoji_google_to_unified($data); # Google Android devices |
14
|
|
|
$data = emoji_unified_to_name($data); |
15
|
|
|
$data = emoji_unified_to_key($data); |
16
|
|
|
return $data; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
static public function covertNameToEmoji($data) { |
|
|
|
|
20
|
|
|
$data = emoji_name_to_unified($data); |
21
|
|
|
return $data; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
static public function covertNameToEmpty($data) { |
|
|
|
|
25
|
|
|
$data = emoji_name_to_empty($data); |
26
|
|
|
return $data; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
static public function covertUnifiedToEmpty($data) { |
|
|
|
|
30
|
|
|
$data = emoji_unified_to_empty($data); |
31
|
|
|
return $data; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
static public function covertHtmlToEmoji($data) { |
|
|
|
|
35
|
|
|
$data = emoji_html_to_unified($data); |
36
|
|
|
return $data; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
static private function uniChr($u) { |
|
|
|
|
40
|
|
|
return mb_convert_encoding('&#' . intval($u) . ';', 'UTF-8', 'HTML-ENTITIES'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
static private function uniChrCode($data) { |
|
|
|
|
44
|
|
|
list(, $ord) = unpack('N', mb_convert_encoding($data, 'UCS-4BE', 'UTF-8')); |
45
|
|
|
return $ord; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
static public function textWithEmoji($data) { |
|
|
|
|
49
|
|
|
if($data=="") return false; |
50
|
|
|
|
51
|
|
|
$data = LaravelEmoji::covertEmojiToName($data); |
52
|
|
|
$data = LaravelEmoji::covertNameToEmoji($data); |
53
|
|
|
|
54
|
|
|
$CPString = ""; |
55
|
|
|
$haut = 0; |
56
|
|
|
$n = 0; |
|
|
|
|
57
|
|
|
|
58
|
|
|
$strLen = mb_strlen($data, 'UTF-8'); |
59
|
|
|
for($i=0; $i<$strLen; $i++) { |
60
|
|
|
$char = mb_substr($data, $i, 1, 'UTF-8'); |
61
|
|
|
$charCode = LaravelEmoji::uniChrCode($char); |
62
|
|
|
|
63
|
|
|
if($charCode>1000) { |
64
|
|
|
|
65
|
|
|
if ($haut != 0) { |
66
|
|
|
if (0xDC00 <= $charCode&& $charCode<= 0xDFFF) { |
67
|
|
|
$CPString .= ' imgFront'. strtolower(dechex(0x10000 + (($haut - 0xD800) << 10) + ($charCode - 0xDC00))) . 'imgBack '; |
68
|
|
|
$haut = 0; |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
else { |
72
|
|
|
$CPString .= 'Error2' . strtolower(dechex($haut)) . '!'; |
73
|
|
|
$haut = 0; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (0xD800 <= $charCode && $charCode <= 0xDBFF) { |
78
|
|
|
$haut = $charCode; |
79
|
|
|
} |
80
|
|
|
else { |
81
|
|
|
$CPString .= ' imgFront'. strtolower(dechex($charCode)) . 'imgBack '; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
else { |
85
|
|
|
$CPString .= LaravelEmoji::uniChr($charCode); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$CPString = str_replace('imgFront', '<img class="emoji-img" width="16" src="http://twemoji.maxcdn.com/16x16/', $CPString); |
90
|
|
|
$CPString = str_replace('imgBack', '.png"/>', $CPString); |
91
|
|
|
return $CPString; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.