2016-08-15 2 views
-3

나는 정규식에 익숙하지만 지금은 배울 시간이 없다. eregi ("^ ..? $", $ file)를 preg_match()로 변환해야하지만 나는 ' 그것을하는 방법을 안다면, 아무도 나를 도울 수 있습니까?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

스택에 대한 빠른 견해는 같은 질문을 한 다른 사람들을 나타낼 것입니다. 아마도 도움이 될 것입니다. http://stackoverflow.com/questions/2501494/how-to-convert-eregi-to-preg-match? rq = 1 – RamRaider

+0

누군가가 당신을 위해 코드를 작성하기를 기다리는 대신 정규 표현식을 배우는 것이 좋습니다. 아주 간단합니다. – lorond

답변

0

변경을 eregi("^..?$", $file) eregi에서

preg_match("/^\.\.?$/i", $file)하는 방법 정규 표현식을 위해 오프너와 클로저를 넣을 필요는 없지만, 프리젠 테이션을 사용하면 시작과 끝의 두 개의 슬래시가 필요합니다.

기본적으로이 정규식은로 시작하는 모든 파일 이름과 일치합니다. 거기에서 끝내거나 다른 것을 가지십시오. 그리고 거기서 끝나면 파일 과 일치 할 것입니다.는 ..

이 일의 빠른 방법은 당신이하지 않기 때문에 그들은 좋은 코드 구조에 대한 나쁜 때문에 continuebreak 문을 사용하지 않도록하려고 시도하는이

$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++; 
    } 
} 

같다 코드를 볼 때 왜 거기에 있는지에 대한 분명한 생각을 가지고 있습니다.

0

변환 된 preg_match는 다음과 같이 표시 될 수 있습니다.

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

추 신 :이 웹 사이트의 정규 시험에 사용합니다. https://regex101.com/

관련 문제