forked from wishgroupe/fatfree-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utf.php
199 lines (176 loc) · 4.61 KB
/
utf.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/*
Copyright (c) 2009-2019 F3::Factory/Bong Cosca, All rights reserved.
This file is part of the Fat-Free Framework (http://fatfreeframework.com).
This is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or later.
Fat-Free Framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
*/
//! Unicode string manager
class UTF extends Prefab {
/**
* Get string length
* @return int
* @param $str string
**/
function strlen($str) {
preg_match_all('/./us',$str,$parts);
return count($parts[0]);
}
/**
* Reverse a string
* @return string
* @param $str string
**/
function strrev($str) {
preg_match_all('/./us',$str,$parts);
return implode('',array_reverse($parts[0]));
}
/**
* Find position of first occurrence of a string (case-insensitive)
* @return int|FALSE
* @param $stack string
* @param $needle string
* @param $ofs int
**/
function stripos($stack,$needle,$ofs=0) {
return $this->strpos($stack,$needle,$ofs,TRUE);
}
/**
* Find position of first occurrence of a string
* @return int|FALSE
* @param $stack string
* @param $needle string
* @param $ofs int
* @param $case bool
**/
function strpos($stack,$needle,$ofs=0,$case=FALSE) {
return preg_match('/^(.{'.$ofs.'}.*?)'.
preg_quote($needle,'/').'/us'.($case?'i':''),$stack,$match)?
$this->strlen($match[1]):FALSE;
}
/**
* Returns part of haystack string from the first occurrence of
* needle to the end of haystack (case-insensitive)
* @return string|FALSE
* @param $stack string
* @param $needle string
* @param $before bool
**/
function stristr($stack,$needle,$before=FALSE) {
return $this->strstr($stack,$needle,$before,TRUE);
}
/**
* Returns part of haystack string from the first occurrence of
* needle to the end of haystack
* @return string|FALSE
* @param $stack string
* @param $needle string
* @param $before bool
* @param $case bool
**/
function strstr($stack,$needle,$before=FALSE,$case=FALSE) {
if (!$needle)
return FALSE;
preg_match('/^(.*?)'.preg_quote($needle,'/').'/us'.($case?'i':''),
$stack,$match);
return isset($match[1])?
($before?
$match[1]:
$this->substr($stack,$this->strlen($match[1]))):
FALSE;
}
/**
* Return part of a string
* @return string|FALSE
* @param $str string
* @param $start int
* @param $len int
**/
function substr($str,$start,$len=0) {
if ($start<0)
$start=$this->strlen($str)+$start;
if (!$len)
$len=$this->strlen($str)-$start;
return preg_match('/^.{'.$start.'}(.{0,'.$len.'})/us',$str,$match)?
$match[1]:FALSE;
}
/**
* Count the number of substring occurrences
* @return int
* @param $stack string
* @param $needle string
**/
function substr_count($stack,$needle) {
preg_match_all('/'.preg_quote($needle,'/').'/us',$stack,
$matches,PREG_SET_ORDER);
return count($matches);
}
/**
* Strip whitespaces from the beginning of a string
* @return string
* @param $str string
**/
function ltrim($str) {
return preg_replace('/^[\pZ\pC]+/u','',$str);
}
/**
* Strip whitespaces from the end of a string
* @return string
* @param $str string
**/
function rtrim($str) {
return preg_replace('/[\pZ\pC]+$/u','',$str);
}
/**
* Strip whitespaces from the beginning and end of a string
* @return string
* @param $str string
**/
function trim($str) {
return preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u','',$str);
}
/**
* Return UTF-8 byte order mark
* @return string
**/
function bom() {
return chr(0xef).chr(0xbb).chr(0xbf);
}
/**
* Convert code points to Unicode symbols
* @return string
* @param $str string
**/
function translate($str) {
return html_entity_decode(
preg_replace('/\\\\u([[:xdigit:]]+)/i','&#x\1;',$str));
}
/**
* Translate emoji tokens to Unicode font-supported symbols
* @return string
* @param $str string
**/
function emojify($str) {
$map=[
':('=>'\u2639', // frown
':)'=>'\u263a', // smile
'<3'=>'\u2665', // heart
':D'=>'\u1f603', // grin
'XD'=>'\u1f606', // laugh
';)'=>'\u1f609', // wink
':P'=>'\u1f60b', // tongue
':,'=>'\u1f60f', // think
':/'=>'\u1f623', // skeptic
'8O'=>'\u1f632', // oops
]+Base::instance()->EMOJI;
return $this->translate(str_replace(array_keys($map),
array_values($map),$str));
}
}