2016-08-15 36 views
-3

我是新來的正則表達式,但我沒有現在的時間來了解它, 但我需要將eregi(「^ ..?$」,$ file)轉換爲preg_match(),但是我 don'不知道該怎麼做,任何人都可以幫助我嗎?將PHP eregi轉換爲preg_match,我該怎麼做?

也給我的工作原理有一點了解也將是 不錯的:)

的一段代碼:

$fileCount = 0; 
while ($file = readdir($dh) and $fileCount < 5){ 
    if (eregi("^..?$", $file)) { 
     continue; 
    } 
    $open = "./xml/".$file; 
    $xml = domxml_open_file($open); 

    //we need to pull out all the things from this file that we will need to 
    //build our links 
    $root = $xml->root(); 
    $stat_array = $root->get_elements_by_tagname("status"); 
    $status = extractText($stat_array); 

    $ab_array = $root->get_elements_by_tagname("abstract"); 
    $abstract = extractText($ab_array); 

    $h_array = $root->get_elements_by_tagname("headline"); 
    $headline = extractText($h_array); 

    if ($status != "live"){ 
     continue; 
    } 
    echo "<tr valign=top><td>"; 
    echo "<a href=\"showArticle.php?file=".$file . "\">".$headline . "</a><br>"; 
    echo $abstract; 
    echo "</td></tr>"; 

    $fileCount++; 
} 
+0

您可能需要花時間,也許我們沒有任何餘地 – RiggsFolly

+0

快速查看Stack會揭示其他人提出的問題,也許這可能有助於http://stackoverflow.com/questions/2501494/how-to-convert-eregi-to-preg-match?rq = 1 – RamRaider

+0

不要等人爲你寫代碼,你最好開始學習正則表達式。這很簡單。 – lorond

回答

0

變化eregi("^..?$", $file)preg_match("/^\.\.?$/i", $file)

在eregi你不必爲正則表達式放置opener和closure,但是preg必須(這是開始和結束時的這兩個斜槓)。

基本上這個正則表達式匹配所有以開頭的文件名。並在那裏結束或有另一個。然後在那裏結束,所以它會匹配文件...

這樣做的更快的方法是這樣的

$fileCount = 0; 
while ($file = readdir($dh) and $fileCount < 5){ 
    if($file != "." && $file != "..") { 
     $open = "./xml/".$file; 
     $xml = domxml_open_file($open); 

     //we need to pull out all the things from this file that we will need to 
     //build our links 
     $root = $xml->root(); 
     $stat_array = $root->get_elements_by_tagname("status"); 
     $status = extractText($stat_array); 

     $ab_array = $root->get_elements_by_tagname("abstract"); 
     $abstract = extractText($ab_array); 

     $h_array = $root->get_elements_by_tagname("headline"); 
     $headline = extractText($h_array); 

     if ($status != "live"){ 
      continue; 
     } 
     echo "<tr valign=top><td>"; 
     echo "<a href=\"showArticle.php?file=".$file . "\">".$headline . "</a><br>"; 
     echo $abstract; 
     echo "</td></tr>"; 

     $fileCount++; 
    } 
} 

你想嘗試,因爲他們是壞的好的代碼結構,因爲你不這樣做,以避免使用continuebreak聲明清楚地知道爲什麼當你看代碼時他們在那裏。

0

轉換後的preg_match可以看起來像這樣。

if (preg_match("/\^|\.\.|\?|\$.*/", $file)) { 
    continue; 
} 

PS:我使用正則表達式測試本網站。 https://regex101.com/