PHP/REGEX: If any matching $_POST item is not empty

Using PHP/REGEX, how can I determine if there is any $_POST item that is not empty that matches this pattern:
hello-123
It should always begin with 'hello-' and should end with any positive integer.

Obviously this doe not work, but it demonstrates what I need:
if(!empty( $_POST['/hello\-\d*/'] )) {
 echo 'There is at least one matching $_POST item that is not empty.';
}

Open in new window

LVL 10
skijAsked:
Who is Participating?
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.

Ray PaseurCommented:
<?php // demo/temp_skij.php

/**
 * http://www.experts-exchange.com/questions/28710773/PHP-REGEX-If-any-matching-POST-item-is-not-empty.html
 */
error_reporting(E_ALL);
echo '<pre>';

// A REGULAR EXPRESSION TO FIND -DIGIT(S) AT THE END OF A STRING
$rgx
= '#'        // REGEX DELIMITER
. '^hello'   // STARTS WITH 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-']    = 'Omit';
$_POST['hello-x']   = 'Omit';
$_POST['hello-3x']  = 'Omit';

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

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

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

Open in new window

0
skijAuthor Commented:
I am looking for something that could be easily and quickly used in an if statement along with other criteria like this:
if(!empty($_POST['id']) && !empty($_POST['comments']) && !empty( $_POST['/hello\-\d*/']) ) {
 //
}

Open in new window

0
Ray PaseurCommented:
How about this way?  One line test.  

FWIW, I never would write code like this.  Compound statements are an anti-practice because you have to take them apart to debug them.  So I recommend you think carefully about the readability and maintainability of the code.  That's far more important than doing it all in one line!

I'll leave it to you to collapse the regular expression into a one-liner.  The one you're using above has some holes in it, but I think this one implements all of the rules you need. :-)
<?php // demo/temp_skij.php

/**
 * http://www.experts-exchange.com/questions/28710773/PHP-REGEX-If-any-matching-POST-item-is-not-empty.html
 */
error_reporting(E_ALL);
echo '<pre>';

// A REGULAR EXPRESSION TO FIND -DIGIT(S) AT THE END OF A STRING
$rgx
= '#'        // REGEX DELIMITER
. '^hello'   // STARTS WITH 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-']    = 'Omit';
$_POST['hello-x']   = 'Omit';
$_POST['hello-3x']  = 'Omit';

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

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

foreach ($_POST as $key => $value)
{
    // A ONE-LINE TEST FOR TWO CONDITIONS
    if ( preg_match($rgx, $key) && !empty($_POST[$key]) ) echo PHP_EOL . "$key => $value";
}

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

skijAuthor Commented:
That code is still inside a foreach loop and it does not test if the value is empty.   I need something that could be used with this and it does not make sense to test of the other items are empty 100 times.
if(!empty($_POST['id']) && !empty($_POST['comments']) && !empty( $_POST['/hello\-\d*/']) ) {
 //
}

Open in new window

0
Julian HansenCommented:
This seems related to this question http://www.experts-exchange.com/questions/28710755/PHP-REGEX-Posted-Items-Ending-with-dash-number.html

I need something that could be used with this and it does not make sense to test of the other items are empty 100 times.

The problem is you have
$_POST = array(
  'item1' => 'value1',
  'item2' => 'value2'
  'item3' => 'value3'
  ...);

Open in new window

The only way to test an array for a value is to walk through it. A key / hash / index will take you to an element but it has to be an exact match.

One outside option here is to change the bit that is sending the data and instead of sending
hello-1, hello-123 etc it sends the data as an array hello[].

For example
<input type="text" name="hello[1]" />
<input type="text" name="hello[123]" />
<input type="text" name="hello[4]" />

Open in new window

When posted will come through in the post as $_POST['hello'] which PHP will then interpret as an array.

You can then easily do if (isset($_POST['hello']))
But as I said - outside chance  without knowing more about your code difficult to say if it is a fit. If not then you are stuck with the loop.
0

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
skijAuthor Commented:
What about creating a new array containing matching items and then testing to see if any of those items in the new array are not empty?  How could that be implemented?
0
Ray PaseurCommented:
creating a new array containing matching items and then testing to see if any of those items in the new array are not empty?
<?php // demo/temp_skij.php

/**
 * http://www.experts-exchange.com/questions/28710773/PHP-REGEX-If-any-matching-POST-item-is-not-empty.html
 */
error_reporting(E_ALL);
echo '<pre>';

// A REGULAR EXPRESSION TO FIND -DIGIT(S) AT THE END OF A STRING
$rgx
= '#'        // REGEX DELIMITER
. '^hello'   // STARTS WITH 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-']    = 'Omit';
$_POST['hello-x']   = 'Omit';
$_POST['hello-3x']  = 'Omit';

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

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

// CREATE A NEW ARRAY
$new = [];

// COPY ONLY THE MATCHING ITEMS
foreach ($_POST as $key => $value)
{
    if (preg_match($rgx, $key)) $new[$key] = $value;
}

// NOW TEST FOR EMPTY ITEMS
foreach ($new as $key => $value)
{
    if (empty($value)) continue;
    echo PHP_EOL . "$key => $value";
}

Open in new window

0
Ray PaseurCommented:
You may be overestimating the cost and weight of using the PHP iterators.  You might want to attach a timer to the script and see how long (or better said, "how short") it takes to iterate over all of the elements in the $_POST array.  Or just see Anti-Practice #26 if you don't want to do the research.  The important parts here are these:

Every key in $_POST is unique
Every key in $_POST must be examined for a pattern match
Keys that match the pattern must be used to acquire the corresponding values
The acquired values must be examined for not-null

You might want to think about writing your own function to filter the $_POST array and return a filtered array.  Then you would get your in-line code down to a single statement, and you would still have all of the functionality you need in a clearly-written code block.
<?php // demo/temp_skij.php

/**
 * http://www.experts-exchange.com/questions/28710773/PHP-REGEX-If-any-matching-POST-item-is-not-empty.html
 */
error_reporting(E_ALL);
echo '<pre>';


// A SPECIALIZED FILTER FUNCTION
function filter_post($post)
{
    $return_array = [];

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

    foreach ($post as $key => $value)
    {
        if (preg_match($rgx, $key))
        {
            if (!empty($value)) $return_array[$key] = $value;
        }
    }

    return $return_array;
}


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

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

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


// USE THE FILTER FUNCTION -- ONE LINE OF CODE
$redacted_data = filter_post($_POST);

// SHOW THE WORK PRODUCT
print_r($redacted_data);

Open in new window

[Edited to correct tab-indent spacing]
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.