php创建目录,删除文件夹及查找不合法的目录并删除

severaltipsaboutRegularExpressions

//$pattern = “/file-([0-2]d{3})-([1-9]d?)/”;
这个正则表达式,对文件夹的月日进行捕获,再进一步判断处理 

1.processforgreedy

02 for($i =2005;$i<=2009;$i++) 

Bydefault,thequantifiersaregreedy,thatis,they

03 { 

matchasmuchaspossible(uptothemaximumnumberofper-

04 for($j = 1;$j<30;$j++) 

mittedtimes),withoutcausingtherestofthepatternto

05 { 

fail.Theclassicexampleofwherethisgivesproblemsisin

06 $dest_dir = “text/file-“.$i.”-$j”; 

tryingtomatchcommentsinCprograms.Theseappearbetween

07 if(!is_dir($dest_dir)) mkdir($dest_dir,0777); 

thesequences/*and*/andwithinthesequence,individual

08 } 

*and/charactersmayappear.AnattempttomatchCcom-

09 } 

mentsbyapplyingthepattern

10 //创建一些不满足条件的文件夹 

//*.*/*/

11   

tothestring

12 for($t = 0;$t<10;$t++) 

/*firstcommand*/notcomment/*secondcomment*/

13 { 

澳门新葡亰手机版,fails,becauseitmatchestheentirestringduetothe

14 $dest_dir = “text/file-“.$t; 

greedinessofthe.*item.

15 if(!is_dir($dest_dir)) mkdir($dest_dir,0777); 

However,ifaquantifierisfollowedbyaquestionmark,

16 } 

thenitceasestobegreedy,andinsteadmatchestheminimum

17   

numberoftimespossible,sothepattern

18 //遍历文件夹处理 . 

//*.*?/*/

19 function listFile($dir) 

小结:

20 {  

?与/U有类似功能,但同时出现彼此抵消

21 $a = array(); 

如下:

22 $handle = opendir($dir); 

$a=asdf/*asdfaldsfasdf*/asfdasldf;kfldsj*/asfddsaf;

23 while($file = readdir($handle)) 

$pattern=////*.*?/*///;

24 { 

//$pattern=////*.*/*///U;

25 handle_dir($dir,$file,&$a); 

//$pattern=////*.*?/*///U;

26 } 

preg_match($pattern,$a,$match);

27   

print_r($match);

28 echo “the rege_array is “; 

29 print(“<pre>”); 

2.Assertions

30 print_r($a); 

/w+(?=;)

31 print(“</pre>”); 

matchesawordfollowedbyasemicolon,butdoesnotinclude

32 } 

thesemicoloninthematch,and

33   

foo(?!bar)

34 function handle_dir($dir,$file,$a) 

matchesanyoccurrenceoffoothatisnotfollowedby

35 { 

bar.Notethattheapparentlysimilarpattern

36