How to do "less than" with this...?

i-CONICA
i-CONICA used Ask the Experts™
on
Hi,

I'm using this code, but I need it to return true only if it's detected less than IE8...


Could someone help modify this to do this?

Thanks. :D
function detect_ie(){
    if(isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 8.0') != false)){
        return true;
}
    else{
        return false;
}
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Try this:
$explode_set1 = explode('(',$_SERVER['HTTP_USER_AGENT']);
$explode_set2 = explode(';',$explode_set1[1]);
foreach ($explode_set2 as $element) {
  if (ereg("MSIE",$element)) {
    $this_element = explode(' ',$element);
    if ($this_element < "8.0") {
      echo "true";
      break;
    } else {
      echo "false";
      break;
    }
  }
}

Open in new window

Commented:
Yuck... I rushed that. Here is the correct code:
$explode_set1 = explode('(',$_SERVER['HTTP_USER_AGENT']);
$explode_set2 = explode(';',$explode_set1[1]);
foreach ($explode_set2 as $element) {
  if (ereg("MSIE",$element)) {
    $this_element = trim($this_element);
    $this_element = explode(' ',$element);
    if ($this_element[2] < "8") {
      echo "true";
      break;
    } else {
      echo "false";
      break;
    }
  }
}

Open in new window

Author

Commented:
I was thinking a more simple solution would be possible? :(

Like maybe a substr on MSI 8.0 to take it to character 5 which is the number 8, then compare to see if it's less than 8...

Would that be possible? Any implications in doing it that way? :S
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Commented:
Have you tried using the get_browser function?
http://php.net/manual/en/function.get-browser.php

Author

Commented:
Yeah, that'd be ideal, but it needs the browercap.ini file installed on the system and that's 300KB to load... a bit heavy to use for such a tiny feature... :(

Commented:
Off the top of my head the above two solutions are all I can come up with.

I don't see how substr would work because the 'HTTP_USER_AGENT' variable would look different for every browser.

Guess you need to decide how important this feature is to you and whether you're willing to use a solution like the one provided above to accomplish it.

Author

Commented:
"I don't see how substr would work because the 'HTTP_USER_AGENT' variable would look different for every browser."

It's either going to be MSIE 6.0, MSIE 7.0, MSIE 8.0, I think... So I could use stristr to find the MSIE part, plus 4 characters, into a new string and then 5 characters in, would be the version number... Sounds flaky and fiddly I know.. I was hoping something along those lines could be done.. :(
Commented:
Here's my new function that does exactly as needed without any unnecessary code.

Will return true if less than IE 8. Will return false if IE8 or any other browser... :)
function detect_lt_ie8(){
$useragent = $_SERVER["HTTP_USER_AGENT"];
$work = strpos($useragent, 'MSIE');
$work += 5;
$work = substr($useragent, $work, 3);
if(isset($useragent) && (strpos($useragent, 'MSIE') != false) && ($work < 8.0)){
	return true;
}
else{
	return false;
}
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial