954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP substring comparisons...what kinds are there?

I'm working on a php-built report that's supposed to be printed out. It may be used on IE, it may be used on Firefox, I'm not sure which and have no control over the issue. Differences in the output formatting of the two browsers mean I have to be able to trim the report at different places in order to keep everything looking neat. (Firefox should print landscape with 8 records per page, IE portrait with 10.)

I've found out about the HTTP_USER_AGENT variable. But, with both the strstr command and the stristr command, I can't seem to extract 'Firefox' from the Firefox user agent string, which shows up on echo as "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.12) Gecko/20070508 Firefox/1.5.0.12".

The word Firefox is in there; I can see it. So why can my comparisons not seem to find it? And if the strstr and stristr functions don't see it, are there other php functions that might?

EnderX
Posting Shark
999 posts since Aug 2006
Reputation Points: 483
Solved Threads: 1
 

You might check your usage of the strstr() function, it may be a simple typo or something. The following works fine for me.

<?php
$s = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.12) Gecko/20070508 Firefox/1.5.0.12";
echo ($found=strstr($s,"Firefox")) ? $found : "(not found)";
echo "Firefox position: ".strpos($s,"Firefox");

echo "Firefox removed..";

$s = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.12) Gecko/20070508 /1.5.0.12";
echo ($found=strstr($s,"Firefox")) ? $found : "(not found)";
echo "Firefox position: ".strpos($s,"Firefox");
?>

preg_match() is always an option if you need more complicated parsing.

Edit: The above was just a knock-up test demo - not my suggestion for code to check browser type :)

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Or you can also try this script:

- http://mikecherim.com/experiments/support/P_php_browser_sniffer.phps

Basically it uses eregi function to identify browsers and platforms. Bye :)

cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You