|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\Common; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Classe auxiliar para o tratamento de strings |
|
7
|
|
|
* @category NFePHP |
|
8
|
|
|
* @package NFePHP\Common\Strings |
|
9
|
|
|
* @copyright Copyright (c) 2008-2017 |
|
10
|
|
|
* @license http://www.gnu.org/licenses/lesser.html LGPL v3 |
|
11
|
|
|
* @author Roberto L. Machado <linux dot rlm at gmail dot com> |
|
12
|
|
|
* @link http://github.com/nfephp-org/nfephp for the canonical source repository |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
use ForceUTF8\Encoding; |
|
16
|
|
|
|
|
17
|
|
|
class Strings |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Includes missing or unsupported properties in stdClass inputs |
|
22
|
|
|
* and Replace all unsuported chars |
|
23
|
|
|
* |
|
24
|
|
|
* @param \stdClass $std |
|
25
|
|
|
* @param array $possible |
|
26
|
|
|
* @return \stdClass |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function equilizeParameters( |
|
29
|
|
|
\stdClass $std, |
|
30
|
|
|
$possible, |
|
31
|
|
|
$replaceAccentedChars = false |
|
32
|
|
|
) { |
|
33
|
|
|
$arr = get_object_vars($std); |
|
34
|
|
|
foreach ($possible as $key) { |
|
35
|
|
|
if (!array_key_exists($key, $arr)) { |
|
36
|
|
|
$std->$key = null; |
|
37
|
|
|
} else { |
|
38
|
|
|
if (is_string($std->$key)) { |
|
39
|
|
|
$std->$key = trim(self::replaceUnacceptableCharacters($std->$key)); |
|
40
|
|
|
if ($replaceAccentedChars) { |
|
41
|
|
|
$std->$key = self::toASCII($std->$key); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
return $std; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Replace all specials characters from string and retuns only 128 basics |
|
51
|
|
|
* NOTE: only for UTF-8 |
|
52
|
|
|
* @param string $string |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public static function replaceSpecialsChars($string) |
|
56
|
|
|
{ |
|
57
|
1 |
|
$string = self::squashCharacters($string); |
|
58
|
1 |
|
$string = str_replace('&', 'e', $string); |
|
59
|
1 |
|
$string = preg_replace("/[^a-zA-Z0-9 @#,-_.;:$%\/]/", "", $string); |
|
60
|
1 |
|
return preg_replace("/[<>]/", "", $string); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Clear inputs for build in XML |
|
65
|
|
|
* Only UTF-8 characters is acceptable |
|
66
|
|
|
* & isolated, less than, greater than, quotation marks and apostrophes |
|
67
|
|
|
* should be replaced by their html equivalent |
|
68
|
|
|
* Carriage Return, Tab and Line Feed is not acceptable in strings |
|
69
|
|
|
* Multiple spaces is not acceptable in strings |
|
70
|
|
|
* And no other control character is acceptable either |
|
71
|
|
|
* @param string|null $input |
|
72
|
|
|
* @return string|null |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public static function replaceUnacceptableCharacters($input) |
|
75
|
|
|
{ |
|
76
|
1 |
|
if (empty($input)) { |
|
77
|
|
|
return $input; |
|
78
|
|
|
} |
|
79
|
|
|
//& isolated, less than, greater than, quotation marks and apostrophes |
|
80
|
|
|
//should be replaced by their html equivalent |
|
81
|
1 |
|
$input = str_replace( |
|
82
|
1 |
|
['& ','<','>','"',"'"], |
|
83
|
1 |
|
['& ','<','>','"','''], |
|
84
|
|
|
$input |
|
85
|
|
|
); |
|
86
|
1 |
|
$input = self::normalize($input); |
|
87
|
1 |
|
return trim($input); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Converts all UTF-8 remains in ASCII |
|
92
|
|
|
* @param string $input |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function toASCII($input) |
|
96
|
|
|
{ |
|
97
|
|
|
$input = self::normalize($input); |
|
98
|
|
|
$input = self::squashCharacters($input); |
|
99
|
|
|
return mb_convert_encoding($input, 'ascii'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Replaces all accented characters of their ASCII equivalents |
|
104
|
|
|
* @param string $input |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
1 |
|
public static function squashCharacters($input) |
|
108
|
|
|
{ |
|
109
|
1 |
|
$input = trim($input); |
|
110
|
1 |
|
$aFind = ['á','à','ã','â','é','ê','í','ó','ô','õ','ú','ü', |
|
111
|
|
|
'ç','Á','À','Ã','Â','É','Ê','Í','Ó','Ô','Õ','Ú','Ü','Ç']; |
|
112
|
1 |
|
$aSubs = ['a','a','a','a','e','e','i','o','o','o','u','u', |
|
113
|
|
|
'c','A','A','A','A','E','E','I','O','O','O','U','U','C']; |
|
114
|
1 |
|
return str_replace($aFind, $aSubs, $input); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Replace all non-UTF-8 chars to UTF-8 |
|
119
|
|
|
* Remove all control chars |
|
120
|
|
|
* Remove all multiple spaces |
|
121
|
|
|
* @param string $input |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
1 |
|
public static function normalize($input) |
|
125
|
|
|
{ |
|
126
|
|
|
//Carriage Return, Tab and Line Feed is not acceptable in strings |
|
127
|
1 |
|
$input = str_replace(["\r","\t","\n"], "", $input); |
|
128
|
|
|
//Multiple spaces is not acceptable in strings |
|
129
|
1 |
|
$input = preg_replace('/(?:\s\s+)/', ' ', $input); |
|
130
|
|
|
//Only UTF-8 characters is acceptable |
|
131
|
1 |
|
$input = Encoding::fixUTF8($input); |
|
132
|
1 |
|
$input = preg_replace( |
|
133
|
|
|
'/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]'. |
|
134
|
|
|
'|[\x00-\x7F][\x80-\xBF]+'. |
|
135
|
|
|
'|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*'. |
|
136
|
|
|
'|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})'. |
|
137
|
1 |
|
'|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S', |
|
138
|
1 |
|
'', |
|
139
|
|
|
$input |
|
140
|
|
|
); |
|
141
|
1 |
|
$input = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]'. |
|
142
|
1 |
|
'|\xED[\xA0-\xBF][\x80-\xBF]/S', '', $input); |
|
143
|
|
|
//And no other control character is acceptable either |
|
144
|
1 |
|
return preg_replace('/[[:cntrl:]]/', '', $input); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Remove all non numeric characters from string |
|
149
|
|
|
* @param string $string |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
1 |
|
public static function onlyNumbers($string) |
|
153
|
|
|
{ |
|
154
|
1 |
|
return preg_replace("/[^0-9]/", "", $string); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Remove unwanted attributes, prefixes, sulfixes and other control |
|
159
|
|
|
* characters like \r \n \s \t |
|
160
|
|
|
* @param string $string |
|
161
|
|
|
* @param boolean $removeEncodingTag remove encoding tag from a xml |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
2 |
|
public static function clearXmlString($string, $removeEncodingTag = false) |
|
165
|
|
|
{ |
|
166
|
|
|
$aFind = array( |
|
167
|
2 |
|
'xmlns:default="http://www.w3.org/2000/09/xmldsig#"', |
|
168
|
|
|
' standalone="no"', |
|
169
|
|
|
'default:', |
|
170
|
|
|
':default', |
|
171
|
|
|
"\n", |
|
172
|
|
|
"\r", |
|
173
|
|
|
"\t" |
|
174
|
|
|
); |
|
175
|
2 |
|
$retXml = str_replace($aFind, "", $string); |
|
176
|
2 |
|
$retXml = preg_replace('/(\>)\s*(\<)/m', '$1$2', $retXml); |
|
177
|
2 |
|
if ($removeEncodingTag) { |
|
178
|
1 |
|
$retXml = self::deleteAllBetween($retXml, '<?xml', '?>'); |
|
179
|
|
|
} |
|
180
|
2 |
|
return $retXml; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Remove all characters between markers |
|
185
|
|
|
* @param string $string |
|
186
|
|
|
* @param string $beginning |
|
187
|
|
|
* @param string $end |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
2 |
|
public static function deleteAllBetween($string, $beginning, $end) |
|
191
|
|
|
{ |
|
192
|
2 |
|
$beginningPos = strpos($string, $beginning); |
|
193
|
2 |
|
$endPos = strpos($string, $end); |
|
194
|
2 |
|
if ($beginningPos === false || $endPos === false) { |
|
195
|
|
|
return $string; |
|
196
|
|
|
} |
|
197
|
2 |
|
$textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos); |
|
198
|
2 |
|
return str_replace($textToDelete, '', $string); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Clears the xml after adding the protocol, removing repeated namespaces |
|
203
|
|
|
* @param string $string |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
1 |
|
public static function clearProtocoledXML($string) |
|
207
|
|
|
{ |
|
208
|
1 |
|
$procXML = self::clearXmlString($string); |
|
209
|
1 |
|
$aApp = array('nfe','cte','mdfe'); |
|
210
|
1 |
|
foreach ($aApp as $app) { |
|
211
|
1 |
|
$procXML = str_replace( |
|
212
|
1 |
|
'xmlns="http://www.portalfiscal.inf.br/'.$app.'" xmlns="http://www.w3.org/2000/09/xmldsig#"', |
|
213
|
1 |
|
'xmlns="http://www.portalfiscal.inf.br/'.$app.'"', |
|
214
|
|
|
$procXML |
|
215
|
|
|
); |
|
216
|
|
|
} |
|
217
|
1 |
|
return $procXML; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Remove some alien chars from txt |
|
222
|
|
|
* @param string $txt |
|
223
|
|
|
* @return string |
|
224
|
|
|
*/ |
|
225
|
1 |
|
public static function removeSomeAlienCharsfromTxt($txt) |
|
226
|
|
|
{ |
|
227
|
|
|
//remove CRs and TABs |
|
228
|
1 |
|
$txt = str_replace(["\r","\t"], "", $txt); |
|
229
|
|
|
//remove multiple spaces |
|
230
|
1 |
|
$txt = preg_replace('/(?:\s\s+)/', ' ', $txt); |
|
231
|
|
|
//remove spaces at begin and end of fields |
|
232
|
1 |
|
$txt = str_replace(["| "," |"], "|", $txt); |
|
233
|
1 |
|
return $txt; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Creates a string ramdomically with the specified length |
|
238
|
|
|
* @param int $length |
|
239
|
|
|
* @return string |
|
240
|
|
|
*/ |
|
241
|
1 |
|
public static function randomString($length) |
|
242
|
|
|
{ |
|
243
|
|
|
$keyspace = '0123456789abcdefghijklmnopqr' |
|
244
|
1 |
|
. 'stuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
245
|
1 |
|
$str = ''; |
|
246
|
1 |
|
$max = mb_strlen($keyspace, '8bit') - 1; |
|
247
|
1 |
|
for ($i = 0; $i < $length; ++$i) { |
|
248
|
1 |
|
$str .= $keyspace[rand(0, $max)]; |
|
249
|
|
|
} |
|
250
|
1 |
|
return $str; |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|