使用正则表达式测试字符串

时间:2015-07-06 15:40:30

标签: php arrays regex string

我有一个字符串数组,想测试一下:

示例:

$strings = array(
    'tmp/install_55984fc72cf6a/admin/views/categories/view.html.php', // Match
    'tmp/install_55984fc72cf6a/admin/views/coupons/index.html',  // Match
    'tmp/install_55984fc72cf6a/admin/views/coupons/tmpl/default.php',  // Match
    'install_55984fc72cf6a/admin/views/coupons/tmpl/configuration.php', // Not match
    'configuration.php',  // Match
    'logs/error.php',  // Match
    'logs/index.html',  // Match
    'logs/joomla_update.php',  // Match
    'folder/logs/log.php',  // Not Match
    'update.php'  // Not match
);

我想定义跳过的文件夹和文件数组

$skip = array('configuration.php', 'logs', 'tmp');
  • logs和tmp将位于字符串的开头;

从数组字符串中删除mathched; 我怎样才能做到这一点? 谢谢!

3 个答案:

答案 0 :(得分:3)

这个问题的字面答案如下

    $strings = array(
        'tmp/install_55984fc72cf6a/admin/views/categories/view.html.php', // Match
        'tmp/install_55984fc72cf6a/admin/views/coupons/index.html',  // Match
        'tmp/install_55984fc72cf6a/admin/views/coupons/tmpl/default.php',  // Match
        'install_55984fc72cf6a/admin/views/coupons/tmpl/configuration.php', // Not match
        'configuration.php',  // Match
        'logs/error.php',  // Match
        'logs/index.html',  // Match
        'logs/joomla_update.php',  // Match
        'folder/logs/log.php',  // Not Match
        'update.php'  // Not match
    );
   $result = preg_grep("/^(logs|tmp|configuration.php)/", $strings, PREG_GREP_INVERT);

的结果
Array
(
    [8] => folder/logs/log.php
    [9] => update.php
)

但是这几乎没有扩展空间。如果您想使用您的方法,文件应用于结尾,文件夹应用于过滤的开头,请尝试此操作。

$strings = array(
    'tmp/install_55984fc72cf6a/admin/views/categories/view.html.php', // Match
    'tmp/install_55984fc72cf6a/admin/views/coupons/index.html',  // Match
    'tmp/install_55984fc72cf6a/admin/views/coupons/tmpl/default.php',  // Match
    'install_55984fc72cf6a/admin/views/coupons/tmpl/configuration.php', // Not match
    'configuration.php',  // Match
    'logs/error.php',  // Match
    'logs/index.html',  // Match
    'logs/joomla_update.php',  // Match
    'folder/logs/log.php',  // Not Match
    'update.php'  // Not match
);


$result = array_filter($strings, function($input){
    $skip = array('configuration.php', 'logs', 'tmp');
    foreach($skip as $toSkip){
        if (strpos($input, $toSkip) !== false) {
            // Directory Skip - Starts with
            if(strrpos($input, $toSkip, -strlen($input)) !== FALSE){
                return false;
            }
        }
    }
    return true;
});

答案 1 :(得分:1)

因为你只从字符串的开头测试,所以正则表达式将是缓慢的解决方案。

比较两个字符串以决定是否保存。并使用array_filter创建新数组

$skip = array('configuration.php', 'logs/', 'tmp/');

function notSkip($str) {
  global $skip;
  foreach($skip as $s) 
    if($s == substr($str, 0, strlen($s))) return false;
  return true;
}

$new = array_filter($strings, 'notSkip');
var_dump($new);

结果

array(
  "install_55984fc72cf6a/admin/views/coupons/tmpl/configuration.php"
  "folder/logs/log.php"
  "update.php"
}

答案 2 :(得分:1)

您可以将 private void SetLocationKeys(CheckedListBox cb, DataTable dt) { int index = 0; foreach (DataRow r in dt.Rows) { if (r.ItemArray[2].ToString() == "1") { cb.SetItemChecked(index, true); } index++; } } preg_grep选项一起使用。但是,使用简单的数组作为过滤器有点天真,因为每个项目都有条件:

PREG_GREP_INVERT

模式细节:

$pattern = '~^(?:logs|tmp)(?:/|$)|(?:^|/)configuration\.php$~S';
$result = preg_grep($pattern, $strings, PREG_GREP_INVERT);

选项~ # pattern delimiter ^ # anchor for the start of the string (?:logs|tmp) # non-capturing group: "logs" or "tmp" (?:/|$) # non-capturing group: a slash or the end of the string | # OR (?:^|/) # non-capturing group: the start of the string or a slash configuration\.php $ # end of the string ~S # pattern delimiter, options* 尝试使用文字字符优化替换。