1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package infrastructure |
4
|
|
|
* @author marius orcsik <[email protected]> |
5
|
|
|
* @date 2010.04.16 |
6
|
|
|
*/ |
7
|
|
|
namespace vsc\infrastructure; |
8
|
|
|
|
9
|
|
|
class StringUtils { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @param string $sString |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
1 |
|
public static function br2nl($sString) { |
16
|
1 |
|
return preg_replace('/<br\/?>/i', "\n", $sString); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $sString |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
1 |
|
public static function stripTags($sString) { |
24
|
1 |
|
return strip_tags($sString); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $sString |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
1 |
|
public static function stripEntities($sString) { |
32
|
1 |
|
return html_entity_decode($sString, ENT_NOQUOTES, 'UTF-8'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $sString |
37
|
|
|
* @param int $iTimes |
38
|
|
|
*/ |
39
|
1 |
|
public static function _echo($sString, $iTimes = 1) { |
40
|
1 |
|
for ($i = 0; $i < $iTimes; $i++) |
41
|
1 |
|
echo $sString; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $sString |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
1 |
|
public static function stripScriptTags($sString) { |
49
|
1 |
|
return preg_replace('/<script.*\/script>/mi', '', (string)$sString); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* returns an end of line, based on the environment |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
1 |
|
public static function nl() { |
57
|
1 |
|
return vsc::isCli() ? "\n" : '<br/>' . "\n"; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Removes all extra spaces from a string |
62
|
|
|
* @param string $sString |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
1 |
|
public static function allTrim($sString) { |
66
|
1 |
|
return trim(preg_replace('/\s+/m', ' ', $sString)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $sString |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
1 |
|
public static function encodeEntities($sString) { |
74
|
1 |
|
return htmlentities($sString, ENT_QUOTES, 'UTF-8'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $sUri |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
2 |
|
public static function formatUri($sUri) { |
82
|
|
|
$aReplaceWhat = array( |
83
|
2 |
|
'/(&([^(amp;)]))/', |
84
|
|
|
'/ {1,}/', |
85
|
|
|
); |
86
|
|
|
$aReplaceWith = array( |
87
|
2 |
|
'&\2', |
88
|
|
|
'+', |
89
|
|
|
); |
90
|
|
|
|
91
|
2 |
|
return preg_replace($aReplaceWhat, $aReplaceWith, $sUri); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $sString |
96
|
|
|
* @param int $iLength |
97
|
|
|
* @param string $sEtc |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
1 |
|
public static function truncate($sString, $iLength, $sEtc = '...') { |
101
|
1 |
|
if ($iLength == 0) |
102
|
|
|
return ''; |
103
|
|
|
|
104
|
1 |
|
if (strlen($sString) > $iLength) { |
105
|
1 |
|
return substr($sString, 0, $iLength) . $sEtc; |
106
|
|
|
} else { |
107
|
1 |
|
return $sString; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param int $val |
113
|
|
|
* @param int $base |
114
|
|
|
* @param string $chars |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
1 |
|
public static function baseEncode($val, $base = 62, $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') { |
118
|
|
|
// can't handle numbers larger than 2^31-1 = 2147483647 |
119
|
1 |
|
$str = ''; |
120
|
|
|
do { |
121
|
1 |
|
$i = $val % $base; |
122
|
1 |
|
$str = $chars[$i] . $str; |
123
|
1 |
|
$val = ($val - $i) / $base; |
124
|
1 |
|
} while ($val > 0); |
125
|
1 |
|
return $str; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $str |
130
|
|
|
* @param int $base |
131
|
|
|
* @param string $chars |
132
|
|
|
* @return int |
|
|
|
|
133
|
|
|
*/ |
134
|
1 |
|
public static function baseDecode($str, $base = 62, $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') { |
135
|
1 |
|
$str = (string)$str; |
136
|
1 |
|
$len = strlen($str); |
137
|
1 |
|
$val = 0; |
138
|
1 |
|
$arr = array_flip(str_split($chars)); |
139
|
1 |
|
for ($i = 0; $i < $len; ++$i) { |
140
|
1 |
|
$val += $arr[$str[$i]] * pow($base, $len - $i - 1); |
141
|
|
|
} |
142
|
1 |
|
return $val; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.