2011-07-18 295 views
1

我想在下面的腳本中添加一個正則表達式過濾器(在array_push之前),以便當它遇到具有相同前綴「_filename」的文件並且其中一個匹配具有'-150x150。 「在文件名的末尾,它會推送該圖像,並使用相同的前綴跳過其他圖像。正則表達式匹配

但是,如果該集合中沒有圖像具有'-150x150'。在文件名,它選擇與根前綴的圖像(_filename.ext)

例如,如果下面的文件駐留在$目錄文件夾:

_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>'; 
    } 
+2

對我來說似乎沒有太多的模式。 – Shef

+0

是的,它不是很明顯,但第一種模式是匹配「 - 」之前的文件名。第二種模式是「 - 」之後的模式 –

回答

0

假設150×150的文件名會經常來的基類後,這裏是你可以使用一個正則表達式:

/^([^-.]+)(?:-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 
     ) 

) 
0

我會做這樣的:

  1. 比賽(.*?)(\-\d+x\d+)?(\.\w+)所有文件名並提取$1基名稱,$2作爲可選分辨率,$3爲後綴;

  2. 走過獨特的基地名稱並檢查您的分辨率是否存在。

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) 

對不起,我沒有使用你的代碼。我想你有足夠的經驗來整合你自己。如果您需要進一步解釋如何適應您的需求,請讓我知道。