@@ -25,11 +25,6 @@ class UserAgentParser
2525 */
2626 const RFC_README = 'https://tools.ietf.org/html/rfc7231#section-5.5.3 ' ;
2727
28- /**
29- * PREG pattern for valid characters
30- */
31- const PREG_PATTERN = '/[^\x21-\x7E]/ ' ;
32-
3328 /**
3429 * Origin product
3530 * @var string
@@ -91,7 +86,7 @@ private function validateProduct()
9186 {
9287 $ this ->blacklistCheck ($ this ->product );
9388 $ this ->originProduct = $ this ->product ;
94- if ($ this ->originProduct !== ($ this ->product = preg_replace ( self :: PREG_PATTERN , '' , $ this ->product ))) {
89+ if ($ this ->originProduct !== ($ this ->product = $ this -> strip ( $ this ->product ))) {
9590 trigger_error ("Product name contains invalid characters. Truncated to ` $ this ->product `. " , E_USER_NOTICE );
9691 }
9792 if (empty ($ this ->product )) {
@@ -122,6 +117,17 @@ private function blacklistCheck($input)
122117 return true ;
123118 }
124119
120+ /**
121+ * Strip invalid characters
122+ *
123+ * @param string|string[] $string
124+ * @return string|string[]|null
125+ */
126+ private function strip ($ string )
127+ {
128+ return preg_replace ('/[^\x21-\x7E]/ ' , '' , $ string );
129+ }
130+
125131 /**
126132 * Validate the Version and it's format
127133 * @link https://tools.ietf.org/html/rfc7231#section-5.5.3
@@ -187,7 +193,7 @@ public function getMostSpecific(array $userAgents)
187193 $ array = [];
188194 foreach ($ userAgents as $ string ) {
189195 // Strip non-US-ASCII characters
190- $ array [$ string ] = strtolower (preg_replace ( self :: PREG_PATTERN , '' , $ string ));
196+ $ array [$ string ] = strtolower ($ this -> strip ( $ string ));
191197 }
192198 foreach (array_map ('strtolower ' , $ this ->getUserAgents ()) as $ generated ) {
193199 if (($ result = array_search ($ generated , $ array )) !== false ) {
0 commit comments