11<?php
22namespace vipnytt ;
33
4+ use Exception ;
5+
46/**
57 * Class UserAgentParser
68 *
@@ -15,9 +17,15 @@ class UserAgentParser
1517 * Constructor
1618 *
1719 * @param string $userAgent
20+ * @throws Exception
1821 */
1922 public function __construct ($ userAgent )
2023 {
24+ if (!extension_loaded ('mbstring ' )) {
25+ throw new Exception ('The extension `mbstring` must be installed and loaded for this library ' );
26+ }
27+ mb_detect_encoding ($ userAgent );
28+
2129 $ this ->userAgent = mb_strtolower (trim ($ userAgent ));
2230 $ this ->explode ();
2331 }
@@ -31,9 +39,9 @@ private function explode()
3139 {
3240 $ this ->groups = [$ this ->userAgent ];
3341 $ this ->groups [] = $ this ->stripVersion ();
34- while (strpos (end ($ this ->groups ), '- ' ) !== false ) {
42+ while (mb_strpos (end ($ this ->groups ), '- ' ) !== false ) {
3543 $ current = end ($ this ->groups );
36- $ this ->groups [] = substr ($ current , 0 , strrpos ($ current , '- ' ));
44+ $ this ->groups [] = mb_substr ($ current , 0 , mb_strrpos ($ current , '- ' ));
3745 }
3846 $ this ->groups = array_unique ($ this ->groups );
3947 }
@@ -45,8 +53,8 @@ private function explode()
4553 */
4654 public function stripVersion ()
4755 {
48- if (strpos ($ this ->userAgent , '/ ' ) !== false ) {
49- return explode ('/ ' , $ this ->userAgent , 2 )[0 ];
56+ if (mb_strpos ($ this ->userAgent , '/ ' ) !== false ) {
57+ return mb_split ('/ ' , $ this ->userAgent , 2 )[0 ];
5058 }
5159 return $ this ->userAgent ;
5260 }
@@ -62,7 +70,7 @@ public function stripVersion()
6270 public function match ($ array , $ fallback = null )
6371 {
6472 foreach ($ this ->groups as $ userAgent ) {
65- if (in_array ($ userAgent , array_map ('strtolower ' , $ array ))) {
73+ if (in_array ($ userAgent , array_map ('mb_strtolower ' , $ array ))) {
6674 return $ userAgent ;
6775 }
6876 }
0 commit comments