Page 1 of 1

reg ex inside of nested for loops, inside of a function

Posted: Mon Feb 11, 2008 9:39 am
by sorin21us
I have this function, take(), which prints a table on the screen.
I call the function like this:
take('#ffffff','green','red','blue','gray','black','#cc00ff','6x6', '#669900');

I want to put patterns to search for hexadecimals
preg_match("/#[0-9ABCDEF]{6}/i",$values)

and for dimensions
preg_match("/[0-9]+x[0-9]+/i",$values)

when the function is called.

My question is how ca I put those pattern, thus in final to have on the screen the table with each color founded in each cell and the number of cell to be by the dimension found (6x6)-a square with 6 cells on horizontal and 6 cells on vertical ?

function take()
{
$funcArgs = func_get_args(); // return an array with the elemets passed to the function
echo '<table border="2px solid black">';
for($row=0; $row < 4; $row++) // outer loop
{
echo '<tr>';
for($col=0; $col < 4; $col++) //inner loop
{
echo '<td>'.$row.$col.'</td>';
}
echo '</tr>';
}
echo '</table>';

}

Re: reg ex inside of nested for loops, inside of a function

Posted: Mon Feb 11, 2008 12:40 pm
by JAM
Not quite sure what you want to do with the colors, but the x*y part could look like this;

Code: Select all

        $funcArgs = func_get_args(); // return an array with the elemets passed to the function
        list($x , $y) = split('x', $funcArgs[7]); // add this to get $x and $y
        echo '<table border="2px solid black">';
        for($row = 0; $row < $y; $row++) // outer loop & the example usage of $y
If you want random color, you can use something like;

Code: Select all

            for($col=0; $col < $x; $col++) //inner loop
            {
                echo '<td style="background-color: '.$funcArgs[mt_rand(0,6)].';">'.$row.$col.'</td>'; // mt_rand() the colors
            }
...but I'm not sure that was what you meant, sorry. :oops:
Image

Re: reg ex inside of nested for loops, inside of a function

Posted: Mon Feb 11, 2008 12:56 pm
by sorin21us
It is correct how you showed me, but if the user call the function take() like this

take('#ffffff','green','6x6','red','blue','gray','black','#cc00ff', '#669900');

how do you know that the dimensions is $funcArgs[2] ?

that is way I used a preg_match

preg_match("/[0-9]+x[0-9]+/i",$values);

but I don't know how use this reg ex inside.

So, if the user call this function like how is above, I must have like this picture (with 6 boxes h and v -that user put, and with the colors one after another one).
:
table_colors.jpg
table_colors.jpg (13.27 KiB) Viewed 310 times

Re: reg ex inside of nested for loops, inside of a function

Posted: Tue Feb 12, 2008 10:43 am
by sorin21us
Can you help me with the last question that I asked you?