There are differences not only between browsers, but also between platforms. This difference is most clear with regard to fonts and font sizes. In the Windows world, you have fonts such as Times New Roman and Courier New. Slight variations of these fonts appear on the Macintosh and Linux/UNIX platforms; they are called Times and Courier. It doesn't end there—the font sizes all are displayed differently. A 10-point font on Macintosh or Linux is sometimes barely legible, but if you bump it up to 11 or 12 point, you're in business. If that same 12-point font is viewed on Windows, however, it might look like your text is trying to take over the world.
So what to do? Use your new pattern-matching skills to extract the platform from the HTTP_USER_AGENT string, and then display platform-specific HTML. As with matching on a keyword to nail down the platform—which you did in the previous section—you also need to know what you're looking for. In the next script, you'll check for the keywords "Win" and "Linux" and print an appropriate style sheet block in your HTML result page.
Open a new file in your text editor, start a PHP block, and use getenv() to place the value of HTTP_USER_AGENT in a variable called $agent:
<?
$agent = getenv("HTTP_USER_AGENT");
Start an if…else statement to find which of the preg_match() functions is true, starting with the search for "Win":
Continue the statement, testing for "Linux":
else if (preg_match("/Linux/i", "$agent")) {
$style = "linux";
}
Create a basic style sheet block for Windows users:
$win_style = "<style type=\"text/css\">p, ul, ol, li
{font-family:Arial;font-size:10pt;font-weight:normal;}
h1 {font-family:Arial;font-size:16pt;font-weight:bold;}
h2 {font-family:Arial;font-size:14pt;font-weight:bold;}
strong {font-family:Arial;font-size:10pt;font-weight:bold;}
em {font-family:Arial;font-size:10pt;font-style:italic;}
</style>";
| Note |
When you use quotation marks inside other quotation marks, the inner pair must be delineated from the outside pair using the escape (\) character (also known as a backslash). |
Create a basic style sheet block for Linux users:
$linux_style = "<style type=\"text/css\">
p, ul, ol, li {font-family:Times;font-size:12pt;font-weight:normal;}
h1 {font-family:Times;font-size:18pt;font-weight:bold;}
h2 {font-family:Times;font-size:16pt;font-weight:bold;}
strong {font-family:Times;font-size:12pt;font-weight:bold;}
em {font-family:Times;font-size:12pt;font-style:italic;}
</style>";
Close your PHP block and add the following HTML:
Type the following PHP code, creating an if…else statement used to print the correct style sheet block:
<?
if ($style == "win") {
echo "$win_style";
} else if ($style == "linux") {
echo "$linux_style";
}
?>
Close the top section of your HTML and start the body:
</HEAD> <BODY>
Type the following HTML to show the use of your style sheet:
<h1 align=center>This is a level 1 heading</h1> <h2 align=center>Look! A level 2 heading</h2> <P align=center>This is a simple paragraph with some <strong>bold</strong> and <em>emphasized</em> text.</P>
Add some more HTML so that the document is valid:
</BODY> </HTML>
Save the file with the name platformmatch.php, and place it in the document root of your web server.
Open your web browser and type http://127.0.0.1/platformmatch.php.
Depending on the web browser you use, you might see a result such as shown in the following two figures.
You can see that the proper style sheet block was printed, based on the result of the platform match. In the next section, you'll move away from pattern matching and work with some of the string functions in PHP to modify form input before displaying it back to the browser.