PHP/REGEX: Posted Items Ending with dash number

Using PHP and REGEX, how can I iterate through all $_POST items where the key ends with -x where x can be any digit?

For example,
$_POST['hello']
should not be included but these should:
$_POST['hello-1']
$_POST['hello-234']
LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Try this
foreach($_POST as $k => $v) {
   if (preg_match("/hello\-\d*/", $k)) {
      echo "YIPPEEEE! Found one [{$v}]<br/>.";
   }
   else {
     echo "Sorry, you don't make the grade [{$k}]... <br/>";
   }
}

Open in new window

skijAuthor Commented:
Would it be better to run the regex directly on the array $_POST before doing the foreach?
Ray PaseurCommented:
Please see http://iconoun.com/demo/temp_skij.php
<?php // demo/temp_skij.php

/**
 * http://www.experts-exchange.com/questions/28710755/PHP-REGEX-Posted-Items-Ending-with-dash-number.html
 */
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');
echo '<pre>';

// A REGULAR EXPRESSION TO FIND -DIGIT(S) AT THE END OF A STRING
$rgx
= '#'        // REGEX DELIMITER
. '^.*?'     // STARTS WITH ANYTHING (OR SHOULD IT START WITH LITERAL 'hello' ?)
. '\-'       // ESCAPED DASH
. '\d'       // DIGIT
. '{1,}'     // ONE OR MORE OF THE DIGITS
. '$'        // AT THE END OF STRING
. '#'        // REGEX DELIMITER
;

// TEST DATA
$_POST['hello']     = 'Omit';

$_POST['hello-1']   = 'Include';
$_POST['hello-234'] = 'Include';


foreach ($_POST as $key => $value)
{
    if (preg_match($rgx, $key)) echo PHP_EOL . "$key => $value";
}

Open in new window

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Ray PaseurCommented:
run the regex directly on the array $_POST before doing the foreach?
Probably not.  You want to test the keys of the $_POST array, and it is best to test them individually so you can see exactly what the script is doing.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
To anyone coming across this answer in the future, please be aware that not all answers posted at E-E contain tested code.  In the case of the code snippet above, a thorough test would have been helpful.  The code snippet below in this comment gives a sample that shows the risk of using that code without fully understanding the code and the way it interacts with the data.

Regular expressions are hard to get right, as XKCD shows.
https://xkcd.com/1171/

The problem here is that the rules requiring a dash and some digit(s) after "hello" are not implemented by the \d* in the regex.  A regex that works correctly is included in this comment.
<?php // demo/temp_skij_erroneous.php

/**
 * http://www.experts-exchange.com/questions/28710755/PHP-REGEX-Posted-Items-Ending-with-dash-number.html#a40959237
 */
error_reporting(E_ALL);
echo '<pre>';

$_POST['hello']     = 'Omit';
$_POST['hello-']    = 'Oops, wrong';
$_POST['hello-x']   = 'Oops, wrong';
$_POST['hello-3-YOU ARE A BOZO']  = 'Oops, wrong';

$_POST['hello-1']   = 'Include';
$_POST['hello-234'] = 'Include';

$_POST['hello-456'] = NULL;


foreach($_POST as $k => $v) {
   if (preg_match("/hello\-\d*/", $k)) {
      echo "YIPPEEEE! Found one $k => [{$v}]<br/>";
   }
   else {
     echo "Sorry, you don't make the grade [{$k}]<br/>";
   }
}

Open in new window

Outputs:
Sorry, you don't make the grade [hello]
YIPPEEEE! Found one hello- => [Oops, wrong]
YIPPEEEE! Found one hello-x => [Oops, wrong]
YIPPEEEE! Found one hello-3-YOU ARE A BOZO => [Oops, wrong]
YIPPEEEE! Found one hello-1 => [Include]
YIPPEEEE! Found one hello-234 => [Include]
YIPPEEEE! Found one hello-456 => []

Open in new window

Julian HansenCommented:
Thanks Ray
Here is the corrected code - small change \d* (match 0 or more digits) to \d+$ (match 1 or more digits and then the end of the string)

foreach($_POST as $k => $v) {
   if (preg_match("/hello\-\d+$/", $k)) {
      echo "YIPPEEEE! Found one [{$v}]<br/>.";
   }
   else {
     echo "Sorry, you don't make the grade [{$k}]... <br/>";
   }
}

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.