Page 1 of 1

Regex Iteration: Every first, second, third of set in array?

Posted: Wed May 07, 2008 5:01 pm
by JAB Creations
Here is post data with blank values...
ce00=
ce01=
ce02=
ce03=
ce10=
ce11=
ce12=
ce13=
ce20=
ce21=
ce22=
ce23=
This code generates the above quote....

Code: Select all

foreach($_POST as $key => $value)
echo $key."=".$value.'<br />';
So how can I create foreach loop that uses a regex that matches each key which ends with '3'. So the output of the above would end up being...
ce03=
ce13=
ce23=

Re: Regex Iteration: Every first, second, third of set in array?

Posted: Wed May 07, 2008 5:32 pm
by JAB Creations
I'm just amazed that I got this to work! :mrgreen:

Code: Select all

foreach($_POST as $key => $value)
if (preg_match("/3$/", $key)) {echo $key."=".$value.'<br />';}

Re: Regex Iteration: Every first, second, third of set in array?

Posted: Thu May 08, 2008 1:12 pm
by GeertDD
Using

Code: Select all

if (substr($key, -1) === '3') {}
probably is faster, though.