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

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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=
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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 />';}
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

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

Post by GeertDD »

Using

Code: Select all

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