2010-07-09 82 views
0

我有兩個數組,一個是OS,比如Ubuntu和Windows,另一個是系統模板,比如Ubuntu 5.3等等等等,而Windows XP SP2等等等等,我需要從系統模板中提取操作系統數組,但它並不總是在開始,有時它在中間或結束。那麼我怎麼能通過一個數組循環,並檢查它是否在另一個數組中,如果是的話告訴我操作系統是什麼。從另一個陣列中的數組搜索字符串

例子。

操作系統列表

$os = array("Ubuntu", "Debian", "Gentoo", "Windows", "Fedora", "CentOS", "CloudLinux", "Slackware"); 

的系統模板列表中的小部分(這將是一個數組)

Ubuntu 8.04 x64 LAMP Installation 
Ubuntu 8.04 x64 MySQL Installation 
Ubuntu 8.04 x64 PHP Installation 
x64 Installation Gentoo 
Basic Installation Ubuntu 8.03 

會導致這給我

Ubuntu 
Ubuntu 
Ubuntu 
Gentoo 
Ubuntu 

感謝

回答

0

做一個正則表達式淘汰之列操作系統的匹配對每個模板的字符串,然後在每個模板字符串映射與正則表達式:

function find_os($template) { 
    $os = array("Ubuntu", "Debian", "Gentoo", "Windows", "Fedora", "CentOS", "CloudLinux", "Slackware"); 
    preg_match('/(' . implode('|', $os) . ')/', $template, $matches); 
    return $matches[1]; 
} 

$results = array_map('find_os', $os_templates); 

array_map()應用find_os()功能,每個模板字符串,以便讓您匹配OS的數組」秒。

0

打電話給你的第一陣列$foo和搜索字詞$os(任意的字符串可能包含操作系統的名稱),然後$result將相應的陣列$foo與名稱從$os

$result = array(); 
for($i = 0; $i < length($foo); $i++){ 
    // set the default result to be "no match" 
    $result[$i] = "no match"; 

    foreach($os as $name){ 
     if(stristr($foo[$i], $name)){ 
      // found a match, replace default value with 
      // the os' name and stop looking 
      $result[$i] = $name; 
      break; 
     } 
    } 
} 
相關問題