How do I process the largest match first in PHP?

Posted by animuson on Stack Overflow See other posts from Stack Overflow or by animuson
Published on 2010-03-22T06:33:30Z Indexed on 2010/03/22 9:31 UTC
Read the original article Hit count: 187

Filed under:
|
|
|

Ok, so I tried searching around first but I didn't exactly know how to word this question or a search phrase. Let me explain.

I have data that looks like this:

<!-- data:start -->
    <!-- 0:start -->
        <!-- 0:start -->0,9<!-- 0:stop -->
        <!-- 1:start -->0,0<!-- 1:stop -->
        <!-- 2:start -->9,0<!-- 2:stop -->
        <!-- 3:start -->9,9<!-- 3:stop -->
        <!-- 4:start -->0,9<!-- 4:stop -->
    <!-- 0:stop -->
    <!-- 1:start -->
        <!-- 0:start -->1,5<!-- 0:stop -->
        <!-- 1:start -->1,6<!-- 1:stop -->
        <!-- 2:start -->3,6<!-- 2:stop -->
        <!-- 3:start -->3,8<!-- 3:stop -->
        <!-- 4:start -->4,8<!-- 4:stop -->
    <!-- 1:stop -->
    <!-- 2:start -->
        <!-- 0:start -->0,7<!-- 0:stop -->
        <!-- 1:start -->1,7<!-- 1:stop -->
    <!-- 2:stop -->
<!-- data:stop -->

So it's basically a bunch of points. Here is the code I'm currently using to try and parse it so that it would create an array like so:

Array (
    0 => Array (
        0 => "0,9",
        1 => "0,0",
        2 => "9,0",
        3 => "9,9",
        4 => "0,9"
    ),
    1 => Array (
        0 => "1,5",
        1 => "1,6",
        2 => "3,6",
        3 => "3,8",
        4 => "4,8"
    ),
    2 => Array (
        0 => "0,7",
        1 => "1,7"
    )
)

However, it is returning an array that looks like this:

Array (
    0 => "0,9",
    1 => "0,0",
    2 => "9,0"
)

Viewing the larger array that I have on my screen, you see that it's setting the first instance of that variable when matching. So how do I get it to find the widest match first and then process the insides. Here is the function I am currently using:

function explosion($text) {
    $number = preg_match_all("/(<!-- ([\w]+):start -->)\n?(.*?)\n?(<!-- \\2:stop -->)/s", $text, $matches, PREG_SET_ORDER);
    if ($number == 0) return $text;
    else unset($item);
    foreach ($matches as $item) if (empty($data[$item[2]])) $data[$item[2]] = $this->explosion($item[3]);
    return $data;
}

I'm sure it will be something stupid and simple that I've overlooked, but that just makes it an easy answer for you I suppose.

© Stack Overflow or respective owner

Related posts about php

Related posts about regex