簡體   English   中英

使用PHP搜索帶有數組值的字符串

[英]search string with array of values with PHP

我正在嘗試搜索文件列表,並僅對名稱包含數組中的幾個值的文件執行工作。 我希望每次有新文件名時都不必遍歷數組。
翼-

$needle = array('blah', 'bleh');
foreach($file_list as $file)
{
    foreach($needle as $need)
       if(strstr($file, $need) !== false)
          {
             // do something...
          }
}

我只需要知道數組中的一個字符串是否在文件名中,而不是字符串的位置。

我想使用類似於strstr()東西,但它不允許將數組用作針。

即 -

if(strstr($haystack, array('blah', 'bleh')))
{
   // do something...
}

我寧願遠離正規表達,似乎是錘子工作的雪橇。 有任何想法嗎?

IMO可以在這里使用正則表達式:

$pattern  = '/' . implode('|', array_map('preg_quote', $needle)) . '/i';

foreach($file_list as $file) {
    if(preg_match($pattern, $file)) {
    // do something
    }
}

參考: preg_quote


這是一種更“創造性”的方法(但它使用內部循環並且很可能更慢,因為您實際上在數組上循環了幾次):

function cstrstr($haystack, $needle) {
    return strstr($haystack, $needle) !== false;
}

foreach($file_list as $file) {
    if(array_sum(array_map('cstrstr', 
                       array_pad(array($file), count($needle), $file), 
                       $needle))) {
        // do something
    }
}

但正則表達式的優點應該是顯而易見的:你必須只創建一次模式而在“有趣”的解決方案中,你總是需要為每個$file創建一個長度count($needle)數組count($needle)

帕特里克

你可以使用in_array()。 例:

foreach($file_list as $file){

   if(in_array($file, $needle)){
     //--------------
     // Needle found
   }
}

您可以在此處找到更多示例: http//php.net/manual/en/function.in-array.php

這將對包含其名稱中所有針的所有文件執行操作

// Loop through files
foreach($files as $file){
    foreach($needles as $needle){
        // if filename does not contain this needle
        if(strpos($file, $needle) === false)
            continue 2; // skip to next file
    }
    // this file matches criteria. do smth on this file
}

如果您只想檢查另一個字符串中是否包含一個字符串,請不要使用preg_match()。 使用strpos()代替,因為它會更快。 http://php.net/manual/en/function.preg-match.php

$needle = array('blah', 'bleh');

foreach ($file_list as $file) {

    $result = $this->searchStrpos($file, $needle);

        if ($result !== false) {

            // do something...
        }
    }

/**
 * 
 * @param String $string String search
 * @param mixed $needle
 * @param Boolean $onlyFirst return true but 
 * @return Boolean Return boolean for search
 */
private function searchStrpos($string, $needle, $onlyFirst = true) {
    if (is_array($needle)) {
        foreach ($needle as $item) {
            $result = strpos($string, $item);
            if ($result !== false && $onlyFirst) {
                break;
            }
        }
    } else {
        $result = strpos($string, $needle);
    }

    return $result;
}

in_array可以正常工作,但是它需要針陣列中的完全匹配。

你可以使用一些散列字符串來implode數組,並使用strstrstrpos的結果 - str變量返回第一個匹配項, pos只返回第一個匹配項的位置 - 確保有一個非常獨特的字符串,同時帶有一個不常見的前綴和后綴。 這是一種快速而骯臟的方法,但它可能只適用於您的情況。

編輯我只是想,你為什么不想使用循環? 它比連接數組中的所有元素要快得多。 你的問題是否有一個使用引用數組找到部分匹配的函數:它不是內置的,所以你最好使用你自己的循環(或者實現一些函數來做,當然使用循環)

if( strstr( implode( " ", $array ), $search )  { //found   }

但是發現循環和返回的函數更快,內存消耗更少。

function searchArrayFast($ array,$ search){foreach($ array as $ a){if(strstr($ a,$ search)){return true; 返回false; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM