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>';
}
reg ex inside of nested for loops, inside of a function
Moderator: General Moderators
Re: reg ex inside of nested for loops, inside of a function
Not quite sure what you want to do with the colors, but the x*y part could look like this;
If you want random color, you can use something like;
...but I'm not sure that was what you meant, sorry. 

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 $yCode: 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
}
Re: reg ex inside of nested for loops, inside of a function
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).
:
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).
:
Re: reg ex inside of nested for loops, inside of a function
Can you help me with the last question that I asked you?