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
|
|
|
* Replace all specials characters from string and retuns only 128 basics |
21
|
|
|
* NOTE: only for UTF-8 |
22
|
|
|
* @param string $string |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
1 |
|
public static function replaceSpecialsChars($string) |
26
|
|
|
{ |
27
|
1 |
|
$string = trim($string); |
28
|
1 |
|
$aFind = ['&','á','à','ã','â','é','ê','í','ó','ô','õ','ú','ü', |
29
|
|
|
'ç','Á','À','Ã','Â','É','Ê','Í','Ó','Ô','Õ','Ú','Ü','Ç']; |
30
|
1 |
|
$aSubs = ['e','a','a','a','a','e','e','i','o','o','o','u','u', |
31
|
|
|
'c','A','A','A','A','E','E','I','O','O','O','U','U','C']; |
32
|
1 |
|
$newstr = str_replace($aFind, $aSubs, $string); |
33
|
1 |
|
$newstr = preg_replace("/[^a-zA-Z0-9 @#,-_.;:$%\/]/", "", $newstr); |
34
|
1 |
|
$newstr = preg_replace("/[<>]/", "", $newstr); |
35
|
1 |
|
return $newstr; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Clear inputs for build in XML |
40
|
|
|
* Only UTF-8 characters is acceptable |
41
|
|
|
* & isolated, less than, greater than, quotation marks and apostrophes |
42
|
|
|
* should be replaced by their html equivalent |
43
|
|
|
* Carriage Return, Tab and Line Feed is not acceptable in strings |
44
|
|
|
* Multiple spaces is not acceptable in strings |
45
|
|
|
* And no other control character is acceptable either |
46
|
|
|
* @param string|null $input |
47
|
|
|
* @return string|null |
48
|
|
|
*/ |
49
|
1 |
|
public static function replaceUnacceptableCharacters($input) |
50
|
|
|
{ |
51
|
1 |
|
if (empty($input)) { |
52
|
|
|
return $input; |
53
|
|
|
} |
54
|
|
|
//& isolated, less than, greater than, quotation marks and apostrophes |
55
|
|
|
//should be replaced by their html equivalent |
56
|
1 |
|
$input = str_replace(['& ','<','>','"',"'"], ['& ','<','>','"','''], $input); |
57
|
|
|
//Carriage Return, Tab and Line Feed is not acceptable in strings |
58
|
1 |
|
$input = str_replace(["\r","\t","\n"], "", $input); |
59
|
|
|
//Multiple spaces is not acceptable in strings |
60
|
1 |
|
$input = preg_replace('/(?:\s\s+)/', ' ', $input); |
61
|
|
|
//Only UTF-8 characters is acceptable |
62
|
1 |
|
$input = Encoding::fixUTF8($input); |
63
|
1 |
|
$input = preg_replace( |
64
|
|
|
'/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]'. |
65
|
|
|
'|[\x00-\x7F][\x80-\xBF]+'. |
66
|
|
|
'|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*'. |
67
|
|
|
'|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})'. |
68
|
1 |
|
'|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S', |
69
|
1 |
|
'', |
70
|
|
|
$input |
71
|
|
|
); |
72
|
1 |
|
$input = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]'. |
73
|
1 |
|
'|\xED[\xA0-\xBF][\x80-\xBF]/S', '', $input); |
74
|
|
|
//And no other control character is acceptable either |
75
|
1 |
|
$input = preg_replace('/[[:cntrl:]]/', '', $input); |
76
|
1 |
|
return trim($input); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Remove all non numeric characters from string |
81
|
|
|
* @param string $string |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
1 |
|
public static function onlyNumbers($string) |
85
|
|
|
{ |
86
|
1 |
|
return preg_replace("/[^0-9]/", "", $string); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Remove unwanted attributes, prefixes, sulfixes and other control |
91
|
|
|
* characters like \r \n \s \t |
92
|
|
|
* @param string $string |
93
|
|
|
* @param boolean $removeEncodingTag remove encoding tag from a xml |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
2 |
|
public static function clearXmlString($string, $removeEncodingTag = false) |
97
|
|
|
{ |
98
|
|
|
$aFind = array( |
99
|
2 |
|
'xmlns:default="http://www.w3.org/2000/09/xmldsig#"', |
100
|
|
|
' standalone="no"', |
101
|
|
|
'default:', |
102
|
|
|
':default', |
103
|
|
|
"\n", |
104
|
|
|
"\r", |
105
|
|
|
"\t" |
106
|
|
|
); |
107
|
2 |
|
$retXml = str_replace($aFind, "", $string); |
108
|
2 |
|
$retXml = preg_replace('/(\>)\s*(\<)/m', '$1$2', $retXml); |
109
|
2 |
|
if ($removeEncodingTag) { |
110
|
1 |
|
$retXml = self::deleteAllBetween($retXml, '<?xml', '?>'); |
111
|
|
|
} |
112
|
2 |
|
return $retXml; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Remove all characters between markers |
117
|
|
|
* @param string $string |
118
|
|
|
* @param string $beginning |
119
|
|
|
* @param string $end |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
2 |
|
public static function deleteAllBetween($string, $beginning, $end) |
123
|
|
|
{ |
124
|
2 |
|
$beginningPos = strpos($string, $beginning); |
125
|
2 |
|
$endPos = strpos($string, $end); |
126
|
2 |
|
if ($beginningPos === false || $endPos === false) { |
127
|
|
|
return $string; |
128
|
|
|
} |
129
|
2 |
|
$textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos); |
130
|
2 |
|
return str_replace($textToDelete, '', $string); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Clears the xml after adding the protocol, removing repeated namespaces |
135
|
|
|
* @param string $string |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
1 |
|
public static function clearProtocoledXML($string) |
139
|
|
|
{ |
140
|
1 |
|
$procXML = self::clearXmlString($string); |
141
|
1 |
|
$aApp = array('nfe','cte','mdfe'); |
142
|
1 |
|
foreach ($aApp as $app) { |
143
|
1 |
|
$procXML = str_replace( |
144
|
1 |
|
'xmlns="http://www.portalfiscal.inf.br/'.$app.'" xmlns="http://www.w3.org/2000/09/xmldsig#"', |
145
|
1 |
|
'xmlns="http://www.portalfiscal.inf.br/'.$app.'"', |
146
|
|
|
$procXML |
147
|
|
|
); |
148
|
|
|
} |
149
|
1 |
|
return $procXML; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Remove some alien chars from txt |
154
|
|
|
* @param string $txt |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
1 |
|
public static function removeSomeAlienCharsfromTxt($txt) |
158
|
|
|
{ |
159
|
|
|
//remove CRs and TABs |
160
|
1 |
|
$txt = str_replace(["\r","\t"], "", $txt); |
161
|
|
|
//remove multiple spaces |
162
|
1 |
|
$txt = preg_replace('/(?:\s\s+)/', ' ', $txt); |
163
|
|
|
//remove spaces at begin and end of fields |
164
|
1 |
|
$txt = str_replace(["| "," |"], "|", $txt); |
165
|
1 |
|
return $txt; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Creates a string ramdomically with the specified length |
170
|
|
|
* @param int $length |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
1 |
|
public static function randomString($length) |
174
|
|
|
{ |
175
|
|
|
$keyspace = '0123456789abcdefghijklmnopqr' |
176
|
1 |
|
. 'stuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
177
|
1 |
|
$str = ''; |
178
|
1 |
|
$max = mb_strlen($keyspace, '8bit') - 1; |
179
|
1 |
|
for ($i = 0; $i < $length; ++$i) { |
180
|
1 |
|
$str .= $keyspace[rand(0, $max)]; |
181
|
|
|
} |
182
|
1 |
|
return $str; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|