Question

re: Home Site = http://mobiledetect.net/
re: this script = Mobile_Detect.php
Download script here: https://github.com/serbanghita/Mobile-Detect

This script functions perfectly detecting the different parameters of a user's device.

However, this is how I am currently detecting these parameters:

// each part of the IF statement is hard-coded = not the way to do this
if($detect->isiOS()){
    $usingOS = 'iOS';
}
if($detect->isAndroidOS()){
    $usingOS = 'Android';
}
echo 'Your OS is: '.$usingOS;

My goal is to use a FOREACH to iterate thru the various arrays in this script to determine a user's device's parameters. I would need to have the "($detect->isXXXXOS())" be dynamic... (which, would be based upon the KEY). The results would display the KEY. But the detection would be based upon the VALUE.

Also, since my web page uses a REQUIRE to access this script... in the Mobile_Script.php script, the arrays are "protected." I think this is also causing me problems (but I don't know for sure).

Any help is appreciated.

Was it helpful?

Solution 2

you can try to use somethin like this:

$OSList = $detect->getOperatingSystems();// will give array of operating system name => match params 

foreach($OSList as $os_name=>$os_params/*unused*/)
{
    $method = 'is'.$os_name;
    if($detect->$method())
    {
        $usingOS = $os_name;
    }
}

OTHER TIPS

In foreach loop you can call dynamic method look like this :

$array = array('Android','Windows','Linux','Mac');

foreach( $array as $value) {
   $method = "is{$value}OS";
   if($detect->$method()) {
      $os = $value;
      echo "Your OS is : {$os}";
   } 
}

Please rearrange your code what you want. I give you an example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top