正则表达式模式匹配

时间:2011-07-18 22:24:12

标签: php regex

我想在下面的脚本中添加一个正则表达式过滤器(就在array_push之前),这样当它遇到具有相同前缀“_filename”且其中一个匹配项为“-150x150”的文件时。在文件名的末尾,它会推送该图像并使用相同的前缀跳过其他图像。

但是,如果该集合中没有图像具有“-150x150”。在文件名中,它选择具有根前缀(_filename.ext)

的图像

例如,如果以下文件驻留在$ dir文件夹中:

_file.png
_file-150x150.png
_file-200x300.png
_someImage.png
_myfile.jpg
_myfile-200x200.jpg

我希望将_file-150x150.png,someImage.png和myfile.jpg推送到数组中,然后跳过其他数据。

$dir = 'wp-content/uploads/';
if(!is_dir($dir)){return;}
$url = get_bloginfo('url').'/wp-content/uploads/';

$imgs = array();
if ($dh = opendir($dir)) 
{
    $myfilter="";
    while (($file = readdir($dh)) !== false) 
        {
        if (!is_dir($file)&& $file != "." && $file != ".." && preg_match("/^[".$myfilter."_].*\.(bmp|jpeg|gif|png|jpg)$/i", $file)) 
            {
                array_push($imgs, $file);
            }
        }
        closedir($dh);
    } else {
        die('cannot open ' . $dir);
    }
    if($imgs) 
    {
        sort($imgs); 
        echo '<div>';
        foreach ($imgs as $idx=>$img) 
        {
            $class = ($idx == count($imgs) - 1 ? ' class="last"' : '');
            echo $prelink.' src="' . $url . $img . '" alt="' .$img . '"' . $class . ' />'.$postlink;
        }
        echo '</div>';
    }

3 个答案:

答案 0 :(得分:0)

我会这样做:

  1. 所有文件名与(.*?)(\-\d+x\d+)?(\.\w+)匹配,并提取$1作为基本名称,$2作为可选解决方案,{{1} }作为后缀;

  2. 浏览唯一的基本名称,并检查是否存在所需的分辨率。

答案 1 :(得分:0)

我发现这样做的最简单方法是将基本文件名和包含150x150的文件添加到关联数组中,并将键作为基本文件名。然后,使用名称中包含150x150的文件覆盖现有值。

    $files = array(
    '_file.png',
    '_file-150x150.png',
    '_file-200x300.png',
    '_someImage.png',
    '_myfile.jpg',
    '_myfile-200x200.jpg'   

    );

    $found = array();


    foreach ($files as $file ) {
        if (preg_match('/(_[^-]*)(-150x150)?\.(\w+)/', $file, $matches)) {
            $base = $matches[1];
            if (isset($found[$base]) && strstr($found[$base],'150x150')) {
                continue;
            } else {
                $found[$base] = $matches[0];
            }
        }

    }

结果var_dump($found)

array
  '_file' => string '_file-150x150.png' (length=17)
  '_someImage' => string '_someImage.png' (length=14)
  '_myfile' => string '_myfile.jpg' (length=11)

抱歉,我没有使用您的代码。我认为你有足够的经验来整合自己。如果您需要进一步解释如何适应您的需求,请告诉我。

答案 2 :(得分:0)

假设150x150文件名总是在基类之后,这里有一个你可以使用的正则表达式:

/^([^-.]+)(?:-150x150)?\.png\b(?!.*\1-150x150\.png\b)/ms

说明:

/
^                        #Match the start of the line
([^-.]+)                 #Match the first part of the file before the -sizeXsize
                         #And store it in the first capturing group.
(?:-150x150)?            #Match 0 or 1 of the desired size.
                         #(?:) is so that you don't store this match.
\.png\b                  #followed by .png
(?!.*\1-150x150\.png\b)  #A negative look ahead for the desired resolution.
                         #i.e. don't match the base if the desired resolution exists.
/ms                      #m is for multiline so that the ^ means start of line rather than start of string.
                         #s is for dotall so that the .* in the lookahead can match newlines.

结果将是这样的:

Array
(
    [0] => Array
        (
            [0] => _file-150x150.png
            [1] => _someImage.png
        )

    [1] => Array
        (
            [0] => _file
            [1] => _someImage
        )

)
相关问题