/
Strings.php
217 lines (199 loc) · 5.76 KB
/
Strings.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* M PHP Framework
*
* @package M
* @subpackage Strings
* @author Arnaud Sellenet <demental at github>
* @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
* @version 0.1
*/
/**
*
* Various string manipulation methods
*
*/
class Strings {
public static function stripify($string,$varCompliant=false) {
$sep = '_';
$string = trim($string, " \t\n\r\0\x0B".$sep);
$string = self::sanitize($string, $varCompliant);
$string = preg_replace('`('.$sep.'+)`',$sep,$string);
$string = preg_replace('`'.$sep.'$`','',$string);
$string = str_replace('?','',$string);
$string = str_replace('_-','_',$string);
$string = str_replace('-_','-',$string);
$string = str_replace('--','-',$string);
$string = str_replace('__','_',$string);
$string = preg_replace('`\s+`','_',$string);
return strtolower($string);
}
public static function sanitize($string, $varCompliant = false)
{
if($varCompliant) {
$string = trim($string);
$string = strtr(utf8_decode($string),utf8_decode('-:+ëËÉÈÀÂÔÊÎÛàâéêèîôûùçò² ,!?&\'"./'),'___eeeeaaoeiuaaeeeiouuco2_________');
} else {
$string = strtr(utf8_decode($string),utf8_decode('ëËÉÈÀÂÔÊÎÛàâéêèîôûùçò² ,!?&\'"./'),'eeeeaaoeiuaaeeeiouuco2---------');
}
return $string;
}
public function unspacify($string)
{
$string = trim($string);
$string = preg_replace('`\s+`u','_',$string);
$string = str_replace('_-','_',$string);
$string = str_replace('-_','-',$string);
$string = str_replace('--','-',$string);
$string = preg_replace('`(_|-)+`u','_',$string);
$string = str_replace('&','and',$string);
return $string;
}
public static function stripLines($text)
{
$text = str_replace(CHR(10),"",$text);
// et celle là aussi :
$text = str_replace(CHR(13),"",$text);
return $text;
}
public static function cleanFileName($name)
{
eregi('(.*)\.([^\.]+)$',$name,$tab);
return self::stripify($tab[1]).'.'.$tab[2];
}
public static function stripifycity($city)
{
$city=self::stripify($city);
$city=str_replace('saint-','st-',$city);
$city = str_replace('-sur-','-s-',$city);
return $city;
}
public static function number_unformat ( $string )
{
$string = preg_replace('`[^0-9]`i','',$string);
return $string;
}
public static function extractEmails ($string)
{
preg_match_all('`([[:alnum:]]([-_.]*[[:alnum:]])*@[[:alnum:]]([-.]?[[:alnum:]])*\.([a-z]{2,4}))`i',$string,$res);
return $res[1];
}
public static function truncate ($string,$length,$end=' ...')
{
$res = preg_replace('`^(.{'.$length.'}[^ ]*) .*$`s','$1'.$end,$string);
return $res;
}
public static function ucsentence ($string){
$string = explode ('.', $string);
$count = count ($string);
for ($i = 0; $i < $count; $i++){
$string[$i] = ucfirst (strtolower(trim ($string[$i])));
if ($i > 0){
if ((ord($string[$i]{0})<48) || (ord($string[$i]{0})>57)) {
$string[$i] = ' ' . $string[$i];
}
}
}
$string = implode ('.', $string);
return $string;
}
public static function create_guid()
{
$microTime = microtime();
list($a_dec, $a_sec) = explode(" ", $microTime);
$dec_hex = sprintf("%x", $a_dec* 1000000);
$sec_hex = sprintf("%x", $a_sec);
ensure_length($dec_hex, 5);
ensure_length($sec_hex, 6);
$guid = "";
$guid .= $dec_hex;
$guid .= create_guid_section(3);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= $sec_hex;
$guid .= create_guid_section(6);
return $guid;
}
/* public static function unaccent($str)
{
$str=strtr(utf8_decode(strtolower($str)),utf8_decode('ûùüàâéêèëîïöôç'),'uuuaaeeeeiiooc');
return $str;
}*/
public static function unaccent($str, $charset='utf-8',$removePunct=true)
{
$str = htmlentities($str, ENT_NOQUOTES, $charset);
$str = preg_replace('#\&([A-za-z])(?:uml|circ|tilde|acute|grave|cedil|ring)\;#', '\1', $str);
$str = preg_replace('#\&([A-za-z]{2})(?:lig)\;#', '\1', $str); // pour les ligatures e.g. 'œ'
if($removePunct) {
$str = preg_replace('#\&[^;]+\;#', '', $str); // supprime les autres caractères
$str = preg_replace('#\W|\s#',' ',$str);
}
return trim($str);
}
public static function autocap($text)
{
while(ereg('[A-Z]{2}',$text)) {
$text = preg_replace('/([A-Z][A-Z]+)/e',"strtolower('\\1')",$text);
}
$text = preg_replace('/\. ?([a-z])/e',"'. '.strtoupper('\\1')",$text);
return ucfirst($text);
}
public static function aucfirst(&$item,$key)
{
$item = ucfirst(trim(strtolower($item)));
}
public static function pascal($string)
{
$string = self::sanitize($string, true);
$string = strtolower($string);
$string = str_replace('_', ' ', $string);
$string = ucwords($string);
$string = str_replace(' ', '', $string);
return $string;
}
public static function camel($string)
{
$string = self::pascal($string);
$string[0] = strtolower($string[0]);
return $string;
}
public static function snake($string)
{
$string = self::sanitize($string, true);
$string = preg_replace('/([A-Z])/', '_$1', $string);
$string = preg_replace('`(_+)`','_',$string);
$string = strtolower($string);
return trim($string, '_');
}
}
function create_guid_section($characters)
{
$return = "";
for($i=0; $i<$characters; $i++)
{
$return .= sprintf("%x", mt_rand(0,15));
}
return $return;
}
function ensure_length(&$string, $length)
{
$strlen = strlen($string);
if($strlen < $length)
{
$string = str_pad($string,$length,"0");
}
else if($strlen > $length)
{
$string = substr($string, 0, $length);
}
}
function microtime_diff($a, $b) {
list($a_dec, $a_sec) = explode(" ", $a);
list($b_dec, $b_sec) = explode(" ", $b);
return $b_sec - $a_sec + $b_dec - $a_dec;
}