user3691346
user3691346

Reputation: 31

Sorting Preg Match All Results

So I've been fighting with this script for a while now, and I cannot seem to get it to sort the results from the Preg Match. What it's supposed to do is retrieve a piece of copy, the I.D> number from the HTML files in the assigned folder, and then list those I.D numbers in the current HTML file and set those echoed ID numbers as links to those other HTML files.

Can anyone give me a hand? I've tried just saying sort($value, 0) etc for the various variables but it doesn't sort them.

<h1 id="pageTitle">
    <?php
        echo(dirname($_SERVER['SCRIPT_NAME']));
    ?>
</h1>
<aside>
    <menu>
<?php

//GET THE DKT NUM
if ($handle = opendir(dirname(__FILE__))) {

    /* This is the correct way to loop over the directory. */
    while (false !== ($entry = readdir($handle))) {
        if (substr($entry, -4) == "html") {
            $file = file_get_contents($entry);
            $pattern = "/<!-- DCN:*[^.]*?-->/"; // FIND HTML COMMENT for Docket Number//

            if (preg_match_all($pattern, $file, $matches)) {
                $value = array_shift($matches);
                $link = ltrim(rtrim($value[0], " -->"), "<!-- DKT: ");
                $tests[] = $link;

                foreach ($tests as $value) {
                    sort($tests,4);
                    echo "<li><a target=\"emailSample\" href=\"$entry\">".$value."</a></li>";
                    unset($tests);
                }
            }
        }
    }

    closedir($handle);
}

?>  
    </menu>

Upvotes: 3

Views: 158

Answers (1)

Pudge601
Pudge601

Reputation: 2068

There are a few issues that I can see with the code, I'll try to cover them all;

Firstly, you don't seem to declare the $tests variable as an array before you start trying to append to it. This can be solved by adding;

$tests = array();

before you perform the preg_match_all.

The next issue is that you're trying to sort the $tests array on each iteration of the loop, which seems unnecessary, so that should probably be moved to before the loop begins.

Also, you're unsetting the $tests variable in the loop, which will delete the entire array. I'm guessing you wanted to remove the current value of the iteration from the array, though since $tests is not used again after this code, I'm not sure what this is for. (It's also slightly inadvisable to delete elements from the array you're currently iterating over, as this can cause odd behaviour in some circumstances, though I think it's OK in a foreach loop in PHP).

Another problem is that you're looping over the elements of $tests every time you add an element to it, which I don't think it what you're aiming for, though I may be wrong.

I've made changes to the main body of your code according the issues I've mentioned above, this is what I've come out with;

while (false !== ($entry = readdir($handle))) {
    if (substr($entry, -4) == "html") {
        $file = file_get_contents($entry);
        $pattern = "/<!-- DCN:*[^.]*?-->/"; // FIND HTML COMMENT for Docket Number//

        $tests = array();

        if (preg_match_all($pattern, $file, $matches)) {
            $value = array_shift($matches);
            $link = ltrim(rtrim($value[0], " -->"), "<!-- DKT: ");
            $tests[] = $link;
        }

        sort($tests);

        foreach ($tests as $key => $value) {
            echo "<li><a target=\"emailSample\" href=\"$entry\">".$value."</a></li>";
        }
    }
}

EDIT

Following the discussion in the comments, here is a possible implementation to allow you to collect all links across all entries, sort by the link, and then print out, using usort to allow you to sort by an element in each item of a multidimensional array;

while (false !== ($entry = readdir($handle))) {
    if (substr($entry, -4) == "html") {
        $file = file_get_contents($entry);
        $pattern = "/<!-- DCN:*[^.]*?-->/"; // FIND HTML COMMENT for Docket Number//

        $tests = array();

        if (preg_match_all($pattern, $file, $matches)) {
            $value = array_shift($matches);
            $link = ltrim(rtrim($value[0], " -->"), "<!-- DKT: ");
            $tests[] = array(
                'entry' => $entry,
                'link'  => $link
            );
        }
    }
}

usort($tests, function($a, $b) {
    return strcmp($a['link'], $b['link']);
});

foreach ($tests as $test) {
    echo "<li><a target=\"emailSample\" href=\"{$test['entry']}\">{$test['value']}</a></li>";
}

Upvotes: 2

Related Questions