2016-08-15 38 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/