繁体   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