tags:

views:

33

answers:

3

Fairly rubbish with PHP, this is a continuation on from my last question.

I have a list of user agents inside an array, and I want an output to be true if the user agent matches one listed inside the array.

This is what I have for a single user agent:

    <?php
        function mediaType(){
           $browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
           $var = 0;
           if ($browser !== false)  { $var = 1; }
           return $var;
        }
    ?>

I want something like this:

<?php

function mediaType(){

    $userAgents = array("iPhone", "Chrome");    
    $browser = $_SERVER['HTTP_USER_AGENT'];
    $var = 0;

    if (in_array($browser, $userAgents)) {
    $var = 1;
    }   

    return $var;        
}

?>

I guess a while loop would be a good option, but I am clueless.

+1  A: 

You should use a foreach loop.

function mediaType(){
   $userAgents = array("iPhone", "Chrome");    
   $browser = $_SERVER['HTTP_USER_AGENT'];

   foreach ($userAgents as $agent) {
     if (strpos($browser, $agent) !== false)
       return 1;
   }
   return 0;
}
KennyTM
+1  A: 

Here's your sweet and simple method and no need for a separate $var:

function mediaType()
{
    $userAgents = array("iPhone", "Chrome");    
    $browser = $_SERVER['HTTP_USER_AGENT'];
    $var = 0;

    foreach($userAgents as $agent)
        if(strpos($browser, $agent) !== FALSE)
            return 1;

    return 0;
}
shamittomar
+1  A: 
function mediaType()
{
    $userAgents = array("iPhone", "Chrome", ....);
    $browser = $_SERVER['HTTP_USER_AGENT'];

    foreach($userAgents AS $userAgent)
    {
        if(preg_match('#' . preg_quote($userAgent, '#') . '#i', $browser))
        {
             return true;
        }
    }

    return false;
}

Edit: Hm I was too late :/ But in comaprison to the other answers I would use preg_match to find the browser :)

faileN
Should not use regex (`preg_match`) unless absolutely required (due to performance issues).
shamittomar
@faileN: You forgotten to escape user agent name with preg_quote.
FractalizeR
Should not use regex unless absolutely required (due to omgwtfthatssupposedtobearegex issues).
salathe
Yeah sorry, I added preg_quote. I only use this solution in here, because of the `i`-modifier to disregard casing. Of course this would also be possible with `stripos`, but the other answers provide just `strpos`. Since `stripos` is only available to PHP 5 or later, the `preg_replace`-solution should also work fine in PHP 4 :)
faileN
Why would you use a regular expression when you don’t need the capabilities of regular expressions? By the way: You’ll get an error if `$userAgent` contains a `#`.
Gumbo
Who said, that I need the capabilities? As I mentioned, it's just PHP 4 compatible. And it doesn't cost as much performance as everbody is always crying about ;) Depends on the server-machine though. Oh and now I added the `delimiter`-parameter to the `preg_quote` :) - So I guess now it's fine.Thank's for the hint with the `#` This ist just the standard-delimiter in our company and I got used to it. Of course, you can use whatever delimiter you want, I don't know what's best here, with the user-agent-string. Thanks.
faileN