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
|
1 |
|
$input = str_replace('&', ' & ', $input); |
80
|
1 |
|
$input = preg_replace('/(?:\s\s+)/', ' ', $input); |
81
|
|
|
//& isolated, less than, greater than, quotation marks and apostrophes |
82
|
|
|
//should be replaced by their html equivalent |
83
|
1 |
|
$input = str_replace( |
84
|
1 |
|
['& ','<','>','"',"'"], |
85
|
1 |
|
['& ','<','>','"','''], |
86
|
|
|
$input |
87
|
|
|
); |
88
|
1 |
|
$input = self::normalize($input); |
89
|
1 |
|
return trim($input); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Converts all UTF-8 remains in ASCII |
94
|
|
|
* @param string $input |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public static function toASCII($input) |
98
|
|
|
{ |
99
|
|
|
$input = self::normalize($input); |
100
|
|
|
$input = self::squashCharacters($input); |
101
|
|
|
return mb_convert_encoding($input, 'ascii'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Replaces all accented characters of their ASCII equivalents |
106
|
|
|
* @param string $input |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
1 |
|
public static function squashCharacters($input) |
110
|
|
|
{ |
111
|
1 |
|
$input = trim($input); |
112
|
1 |
|
$aFind = ['á','à','ã','â','é','ê','í','ó','ô','õ','ö','ú','ü', |
113
|
|
|
'ç','Á','À','Ã','Â','É','Ê','Í','Ó','Ô','Õ','Ö','Ú','Ü','Ç']; |
114
|
1 |
|
$aSubs = ['a','a','a','a','e','e','i','o','o','o','o','u','u', |
115
|
|
|
'c','A','A','A','A','E','E','I','O','O','O','O','U','U','C']; |
116
|
1 |
|
return str_replace($aFind, $aSubs, $input); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Replace all non-UTF-8 chars to UTF-8 |
121
|
|
|
* Remove all control chars |
122
|
|
|
* Remove all multiple spaces |
123
|
|
|
* @param string $input |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
1 |
|
public static function normalize($input) |
127
|
|
|
{ |
128
|
|
|
//Carriage Return, Tab and Line Feed is not acceptable in strings |
129
|
1 |
|
$input = str_replace(["\r","\t","\n"], "", $input); |
130
|
|
|
//Multiple spaces is not acceptable in strings |
131
|
1 |
|
$input = preg_replace('/(?:\s\s+)/', ' ', $input); |
132
|
|
|
//Only UTF-8 characters is acceptable |
133
|
1 |
|
$input = Encoding::fixUTF8($input); |
134
|
1 |
|
$input = preg_replace( |
135
|
|
|
'/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]'. |
136
|
|
|
'|[\x00-\x7F][\x80-\xBF]+'. |
137
|
|
|
'|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*'. |
138
|
|
|
'|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})'. |
139
|
1 |
|
'|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S', |
140
|
1 |
|
'', |
141
|
|
|
$input |
142
|
|
|
); |
143
|
1 |
|
$input = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]'. |
144
|
1 |
|
'|\xED[\xA0-\xBF][\x80-\xBF]/S', '', $input); |
145
|
|
|
//And no other control character is acceptable either |
146
|
1 |
|
return preg_replace('/[[:cntrl:]]/', '', $input); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Remove all non numeric characters from string |
151
|
|
|
* @param string $string |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
1 |
|
public static function onlyNumbers($string) |
155
|
|
|
{ |
156
|
1 |
|
return preg_replace("/[^0-9]/", "", $string); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Remove unwanted attributes, prefixes, sulfixes and other control |
161
|
|
|
* characters like \r \n \s \t |
162
|
|
|
* @param string $string |
163
|
|
|
* @param boolean $removeEncodingTag remove encoding tag from a xml |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
2 |
|
public static function clearXmlString($string, $removeEncodingTag = false) |
167
|
|
|
{ |
168
|
|
|
$aFind = array( |
169
|
2 |
|
'xmlns:default="http://www.w3.org/2000/09/xmldsig#"', |
170
|
|
|
' standalone="no"', |
171
|
|
|
'default:', |
172
|
|
|
':default', |
173
|
|
|
"\n", |
174
|
|
|
"\r", |
175
|
|
|
"\t" |
176
|
|
|
); |
177
|
2 |
|
$retXml = str_replace($aFind, "", $string); |
178
|
2 |
|
$retXml = preg_replace('/(\>)\s*(\<)/m', '$1$2', $retXml); |
179
|
2 |
|
if ($removeEncodingTag) { |
180
|
1 |
|
$retXml = self::deleteAllBetween($retXml, '<?xml', '?>'); |
181
|
|
|
} |
182
|
2 |
|
return $retXml; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Remove all characters between markers |
187
|
|
|
* @param string $string |
188
|
|
|
* @param string $beginning |
189
|
|
|
* @param string $end |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
2 |
|
public static function deleteAllBetween($string, $beginning, $end) |
193
|
|
|
{ |
194
|
2 |
|
$beginningPos = strpos($string, $beginning); |
195
|
2 |
|
$endPos = strpos($string, $end); |
196
|
2 |
|
if ($beginningPos === false || $endPos === false) { |
197
|
|
|
return $string; |
198
|
|
|
} |
199
|
2 |
|
$textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos); |
200
|
2 |
|
return str_replace($textToDelete, '', $string); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Clears the xml after adding the protocol, removing repeated namespaces |
205
|
|
|
* @param string $string |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
1 |
|
public static function clearProtocoledXML($string) |
209
|
|
|
{ |
210
|
1 |
|
$procXML = self::clearXmlString($string); |
211
|
1 |
|
$aApp = array('nfe','cte','mdfe'); |
212
|
1 |
|
foreach ($aApp as $app) { |
213
|
1 |
|
$procXML = str_replace( |
214
|
1 |
|
'xmlns="http://www.portalfiscal.inf.br/'.$app.'" xmlns="http://www.w3.org/2000/09/xmldsig#"', |
215
|
1 |
|
'xmlns="http://www.portalfiscal.inf.br/'.$app.'"', |
216
|
|
|
$procXML |
217
|
|
|
); |
218
|
|
|
} |
219
|
1 |
|
return $procXML; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Remove some alien chars from txt |
224
|
|
|
* @param string $txt |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
1 |
|
public static function removeSomeAlienCharsfromTxt($txt) |
228
|
|
|
{ |
229
|
|
|
//remove CRs and TABs |
230
|
1 |
|
$txt = str_replace(["\r","\t"], "", $txt); |
231
|
|
|
//remove multiple spaces |
232
|
1 |
|
$txt = preg_replace('/(?:\s\s+)/', ' ', $txt); |
233
|
|
|
//remove spaces at begin and end of fields |
234
|
1 |
|
$txt = str_replace(["| "," |"], "|", $txt); |
235
|
1 |
|
return $txt; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Creates a string ramdomically with the specified length |
240
|
|
|
* @param int $length |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
1 |
|
public static function randomString($length) |
244
|
|
|
{ |
245
|
|
|
$keyspace = '0123456789abcdefghijklmnopqr' |
246
|
1 |
|
. 'stuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
247
|
1 |
|
$str = ''; |
248
|
1 |
|
$max = mb_strlen($keyspace, '8bit') - 1; |
249
|
1 |
|
for ($i = 0; $i < $length; ++$i) { |
250
|
1 |
|
$str .= $keyspace[rand(0, $max)]; |
251
|
|
|
} |
252
|
1 |
|
return $str; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|