www.webdeveloper.com
Results 1 to 6 of 6

Thread: dreaded regexp's

  1. #1
    Join Date
    Dec 2002
    Location
    Canada, eh?
    Posts
    228

    dreaded regexp's

    i just recently learned regexps, and so im having a bit of trouble with them...im trying to make a PHP browser detector, and i have most of it worked out, but im trying to figure out a way to find the difference between netscape and mozilla...heres Netscape's user agent, um, thing:

    Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.0.1) Gecko/20020823 Netscape/7.0

    and here's Mozilla (Firebird)'s:

    Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6

    now how do i use a regexp to determine whether a browser is mozilla or not, either searching for "Mozilla" twice, or making sure "Netscape" and "MSIE" are not listed inside the user agent...thanx for the help

  2. #2
    Join Date
    Dec 2002
    Location
    Canada, eh?
    Posts
    228
    cmon people, does no one know regular expressions or something? all i need is to know how to get Mozilla to return true, and Netscape to return false...

  3. #3
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Just search through looking for the string "Netscape":

    PHP Code:
    <?PHP
    $browser 
    $_SERVER["HTTP_USER_AGENT"];
    if (
    preg_match("/Netscape/",$browser)) {
        echo 
    "The browser is Netscape";
    }
    else {
        echo 
    "The browser is not Netscape";
    }
    ?>

  4. #4
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Here's a bit more of a comprehensive browser detection scheme. It works in all the browsers I tested in (IE6, NN7, NN4.7, Mozilla 1.2, and Opera 7)

    PHP Code:
    <?PHP
    $browser 
    $_SERVER["HTTP_USER_AGENT"];
    if (
    preg_match("/MSIE/"$browser)) {
        if (
    preg_match("/Opera/"$browser)) {
            
    $brs "Opera";
        }
        else {
            
    $brs "Internet Explorer";
        }
    }
    else if (
    preg_match("/Mozilla/"$browser)) {
        if (
    preg_match("/(Netscape|NE)/",$browser)) {
            
    $brs "Netscape";
        }
        else {
            
    $brs "Mozilla";
        }
    }
    else {
        
    $brs "unknown";
    }
    echo 
    "The browser is: ".$brs;
    ?>

  5. #5
    Join Date
    Dec 2002
    Location
    Canada, eh?
    Posts
    228
    hey thanx, i guess i just didnt think of it that way...i was thinking of differences between the two, when all i needed was the fact that it wasnt one, so it was the other...

    *smacks head*

    anyways, thanx

  6. #6
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    lol... No problem...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles