-3

I am new to regex but I don't have the time right now to learn it, yet I need to convert eregi("^..?$", $file) to a preg_match() but I don't know how to do it, can anybody help me?

Also giving me a little understanding of how it works would also be nice to have :)

The piece of code:

$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++;
}
  • You might have to make the time, maybe we dont have any to spare – RiggsFolly Aug 15 '16 at 07:54
  • A quick look on Stack would reveal others who have asked the same question, perhaps this might help http://stackoverflow.com/questions/2501494/how-to-convert-eregi-to-preg-match?rq=1 – Professor Abronsius Aug 15 '16 at 07:58
  • Instead of waiting for someone to write code for you, you'd better start learning regex. It quite simple. – lorond Aug 15 '16 at 09:11

2 Answers2

0

Change eregi("^..?$", $file) to preg_match("/^\.\.?$/i", $file)

In eregi you didn't have to put openers and closures for regex, but with preg you have to (those are these two slashes on start and end).

Basically this regex matches all file names that start with . and end there or have another . and then end there, so it would match files . and ..

The faster way of doing this is like this

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

You want to try to avoid using continue and break statements since they are bad for good code structure because you don't have clear idea of why're they there when you look at the code.

b0ne
  • 653
  • 3
  • 10
0

a converted preg_match can be look like this.

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

PS: I using for regex testing this website. https://regex101.com/

Mario Naether
  • 1,122
  • 11
  • 22