SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Weird Little Girl Desdelena's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with strings?

    Hi everyone. Im new to writing scripts and I need some help working with strings. What I want to do is be able to return a browser version from $HTTP_USER_AGENT... but Im really not sure how to even go about this. My guess would be to take a chunk of the string from $HTTP_USER_AGENT inbetween the first and second occurences of a semicolon. But again I dont know how to do this. Can anyone lend a hand or at least point me in the right direction? I tried looking on PHP.net but I didnt find anything that helped me at all. Thanks ahead of time.

  2. #2
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You mean just like:
    PHP Code:
    <?
    echo $HTTP_USER_AGENT;
    ?>
    ?
    - the lid is off the maple syrup again!

  3. #3
    Weird Little Girl Desdelena's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope. Here perhaps this example will help. I am currently using this to get the browser name. I want to do something similar only with the browser version.

    PHP Code:
    if (strstr($HTTP_USER_AGENT,"MSIE")){
        
    $browser "IE";
    }
    elseif (
    strstr($HTTP_USER_AGENT,"Netscape")){
        
    $browser "NN"
    }
    elseif (
    strstr($HTTP_USER_AGENT,"Lynx")){
        
    $browser "Lynx";
    }
    elseif (
    $browser != "IE" || "NN" || "Lynx"){
        
    $browser "Unknown"

  4. #4
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do the same thing but use numbers instead of 'MSIE', 'Netscape'.
    Im not sure but thats the way i would go.
    - the lid is off the maple syrup again!

  5. #5
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know much about http_user_agents but I'll take a shot at this :)

    A regular expression could get that number. For both my browsers the number you would want would be the last number in the string.

    Here's a regular expression that grabs the browser version from this string:Mozilla/4.0 (compatible; MSIE 5.14; Mac_PowerPC)

    Taken out of php tags due to tag messing up content:
    preg_match('/\s(\d+.*);\s/', $HTTP_USER_AGENT, $browser_version);

    Does that help any?

    Perl compatable PHP regular expressions
    http://us2.php.net/manual/en/function.preg-match.php
    Last edited by samsm; May 13, 2002 at 18:54.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  6. #6
    Weird Little Girl Desdelena's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are multiple sets of numbers when $HTTP_USER_AGENT is returned... you cant do the same as I did for MSIE. However Im no expert so if youve got a method that does please do share.

    Sam that is over my head... I read through the page at PHP.net and its still not making much of any sense to me. Could you possibly explain it to me differently? Also the number would not be the last in the string for me: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90). So... wont that mess things up because its in the middle?

  7. #7
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, my search grabs the last set of numbers, so it wouldn't work.

    Does the version number always imdiately follow the browser abreviation?
    Like: blah blah blah MSIE/Netscape [version number is always here] blah blah blah

    If so, post again and we'll figure something out

    I learned about regular expressions in "Learning Perl" an O'Reilly book. That book (which is great) took three chapters to tutor regular expressions and by the end you think anything is possible.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  8. #8
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    if (strstr($HTTP_USER_AGENT,"MSIE")){
    
        $browser = "IE";
        preg_match('/MSIE\s+(\d+[\d\.]*)[\s,;]*/', $HTTP_USER_AGENT, $browser_version);
        echo($browser_version[1]);
    
    }
    
    elseif (strstr($HTTP_USER_AGENT,"Netscape")){
    
        $browser = "NN";
        preg_match('/Netscape\s+(\d+[\d\.]*)[\s,;]*/', $HTTP_USER_AGENT, $browser_version);
        echo($browser_version[1]);
    
    }
    
    elseif (strstr($HTTP_USER_AGENT,"Lynx")){
    
        $browser = "Lynx";
        preg_match('/Lynx\s+(\d+[\d\.]*)[\s,;]*/', $HTTP_USER_AGENT, $browser_version);
        echo($browser_version[1]);
    
    }
    
    elseif ($browser != "IE" || "NN" || "Lynx"){
    
        $browser = "Unknown";
    It works like this:

    Code:
    /           begin the search
    MSIE\s+     find the first occurance of "MSIE" that is followed by whitespace
    (           begin saving the data found here
    \d+         match at least one digit
    [\d\.]*     match 0 or more digits and periods
    )           stop saving data matched
    [\s,;]      match whitespace, commas and semicolons
    If that description does you any good at all I'll be amazed. Like I said, it took me three chapters and that was just the basics However, the code might just work.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  9. #9
    Weird Little Girl Desdelena's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup it always occurs after the browser name. Making some progress here... good good! Thanks so much for your help with this samsm I really appreciate it. What you are saying makes some sense. But I definitely dont fully understand it. Im going to see if I can do some more reading on that preg_match() and Id like to read up on how you knew what all those funny slashes, signs and letters in there (ie: \s+(\d+[\d\.]*)[\s,;]*/) did. How come when you echoed the browser version you put [1] in there? Whats the purpose of that? Is that the whole true/false thing?

    I will try the code out and let you know how it works next time I check back here.

  10. #10
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The slashes (\) are used to create special generic characters. For example \d matches any digit. Alternately [0-9] also matches any digit. \d* matchs zero or more digits, \d+ matches one or more digits, \d? matches zero or one digit.... [\dd] matches one digit or the letter d.

    The [1] was because preg_match saves results of searches to an array, in the case below, $saved_array.

    $example = "Tricky stuff, but good to know.";
    preg_match('/Tricky\s(.*)/', $example, $saved_array);
    var_dump($saved_array);

    [0] holds the entire match
    [1] holds the data from the first set of parenthesis
    [2] second set, etc....
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?


Bookmarks

Posting Permissions

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