2013-11-26 63 views
1

我需要編寫一個正則表達式,它匹配除images文件夾以外的所有文件夾中的所有.html文件。排除文件夾,並使用正則表達式匹配根文件夾中的所有.html模式文件

下面是HTML文件格式

/magazines/sample.html 
/test/index.html 
/test/format_ss1.html 
/test/folder/newstyle_1.html 
/images/two.html 

我需要得到的只有前2個HTML文件,即,我們exluding與結尾的文件 '_ [0-9]' 和「_ss [0-9 ]'和.hmtl文件在圖像文件夾中。

我已成功通過排除3和4,但我無法在圖像文件夾中打開.html文件。

$regex = '/[a-zA-Z0-9\-][^_ss\d][^_\d]+\.html/'; //this will do for 3 and 4 files 

,但我需要exlude圖像文件夾..

我試圖像

$regex = '/[^images\/][a-zA-Z0-9\-][^_ss\d][^_\d]+\.html/'; // not working 

可以有一個人幫助我這個..

回答

2

有一試:

~^(?!/images/)[a-zA-Z0-9/-]+(?!_ss\d|\d)\.html$~ 

在動作:

$arr = array(
'/magazines/sample.html', 
'/test/index.html', 
'/test/format_ss1.html', 
'/test/folder/newstyle_1.html', 
'/images/two.html' 
); 
foreach($arr as $str) { 
    if (preg_match('~^(?!/images/)[a-zA-Z0-9/-]+(?!_ss\d|\d)\.html$~', $str)) 
     echo $str,"\n"; 
} 

輸出:

/magazines/sample.html 
/test/index.html 
+0

感謝響應。但它列出了所有的.html文件.. :( –

+0

@BhanuPrakash:看我的編輯。 – Toto

+0

我已經嘗試過,但沒有...它也包括圖像文件夾太... :(其實我們正在使用這個遷移Drupal7中的東西 –

相關問題