Skip to content

Commit cbf6062

Browse files
committed
Add StringProcessor::isBrowser()
1 parent f900caa commit cbf6062

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/Libraries/Core/StringProcessor.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44

55
class StringProcessor
66
{
7+
/**
8+
* Checks whether the current User Agent is a GUI web browser known by a common string, e.g. Firefox.
9+
*
10+
* @param string $user_agent User Agent to check, or null to check HTTP_USER_AGENT environment variable.
11+
* @return boolean Whether the client is using a GUI web browser or not.
12+
*/
13+
public static function isBrowser(?string $user_agent = null): bool
14+
{
15+
return (1 === \preg_match(
16+
'#(?:AppleWebKit|Chrome|Edge|Firefox|Mozilla|MSIE|Opera|Safari|Trident|WebKit)#i',
17+
$user_agent ?? \getenv('HTTP_USER_AGENT') ?? ''
18+
));
19+
}
20+
721
public static function sanitizeForUrl(array|string $haystack, bool $lowercase = true): array|string
822
{
923
$result = \preg_replace('/[^\da-z]+/im', '-', $haystack);

0 commit comments

Comments
 (0)