What are the best practices for splitting the output of $_SERVER["HTTP_USER_AGENT"] to display specific information like browser or operating system?

When splitting the output of $_SERVER["HTTP_USER_AGENT"], it is recommended to use regular expressions to extract specific information such as the browser or operating system. Regular expressions allow for more precise matching and extraction of the desired data from the user agent string. By using regular expressions, you can easily identify and display the relevant information in a structured manner.

$user_agent = $_SERVER["HTTP_USER_AGENT"];

// Match browser information
if (preg_match('/\b(?:MSIE|Trident|Edge|Chrome|Safari|Firefox)\b/', $user_agent, $matches)) {
    $browser = $matches[0];
    echo "Browser: " . $browser . "<br>";
}

// Match operating system information
if (preg_match('/\b(?:Windows|Macintosh|Linux)\b/', $user_agent, $matches)) {
    $os = $matches[0];
    echo "Operating System: " . $os . "<br>";
}