[Solved] php need assistance with regular expression


Your question is not very clear, but I think you mean a solution like this:

Edited: Now the hole ranges were shown and not only the specified numbers.

<?php
    $string = "0605052&&-5&-7&-8";
    $test="/^([0-9]+)\&+/";
    preg_match($test, $string, $res);

    if (isset($res[1]))
    {
        $nr = $res[1];

        $test="/\&\-([0-9])/";
        preg_match_all($test, $string, $res);

        $result[] = $nr;
        $nrPart = substr($nr, 0, -1);
        $firstPart = substr($nr, -1);

        if (isset($res[1]))
        {
            foreach ($res[1] as &$value)
            {
                if ($firstPart !== false)
                {
                    for ($i=$firstPart+1; $i<=$value; $i++)
                    {
                        $nr = $nrPart . $i;
                        $result[] = $nr;
                    }
                    $firstPart = false;
                }
                else
                {
                    $nr = $nrPart . $value;
                    $result[] = $nr;
                    $firstPart = $value;
                }
            }
        }
        var_dump($result);
    }
?>

This delivers:
result[0] = “0605052”
result[1] = “0605053”
result[2] = “0605054”
result[3] = “0605055”
result[4] = “0605057”
result[5] = “0605058”

2

solved php need assistance with regular expression