2013-07-29 33 views
0

重:首頁網站= http://mobiledetect.net/
重:這個腳本= Mobile_Detect.php
下載腳本的位置:https://github.com/serbanghita/Mobile-DetectPHP - 顯示來自'Detect'數組的結果?

這個腳本功能完美檢測用戶的設備的不同參數。

然而,這是我當前如何檢測這些參數:

// 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; 

我的目標是使用foreach來遍歷各種通陣列在這個腳本來確定用戶的設備的參數。我需要「($ detect-> isXXXXOS())」是動態的......(這將基於KEY)。結果將顯示KEY。但檢測將基於VALUE。

此外,由於我的網頁使用REQUIRE訪問此腳本...在Mobile_Script.php腳本中,數組是「受保護的」。我認爲這也會造成我的問題(但我不確定)。

任何幫助表示讚賞。

+0

要求使得陣列保護?沒有理解你的意思。 – Anigel

+0

爲什麼不使用一系列if/else if語句,一旦找到實際設備就會停止。而foreach將迭代遍歷它們,無論何時發現設備類型 – Paddyd

回答

1

,你可以嘗試使用事端這樣的:

$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; 
    } 
} 
+0

謝謝。但是...有沒有辦法讓您的最高$ OSList(「我,Windows,Linux」)從Mobile_Detect.php腳本填充,而不是硬編碼? – mar2195

+0

我已編輯我的代碼,如果沒有語法錯誤,這應該工作 – insanebits

+0

這是完美的工作!謝謝。順便說一句...如果您嘗試使用Mobile_Detect.php腳本中的$ userAgents數組的上面的代碼,它不起作用。 ??不知道爲什麼。 – mar2195

2

foreach循環中,您可以撥打dynamic method這個樣子的:

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

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

請重新安排你的代碼你想要什麼。我給你一個例子。

+0

謝謝。但是...有沒有辦法讓你的頂級數組('Android','Windows'等)從Mobile_Detect.php腳本填充而不是硬編碼? – mar2195