The Codesee
The Codesee

Reputation: 3783

Why is my preg_match_all not working?

I am using preg_match_all to ensure that a string follows a certain pattern.

It should display 'all conditions are met' becuase the string follows the pattern, but instead, it displays 'conditions net met'.

$order = "item[]=2&item[]=1&item[]=3&item[]=4&item[]=5&item[]=6&item[]=7&item[]=8&item[]=9&item[]=10&item[]=11&item[]=12";
$pattern = "/^(item\[\]=([1-9]|10|11|12))(&(item\[\]=([1-9]|10|11|12))){11}$/";

if(preg_match($pattern, $order)) {

   // check for repetition
   $matches = [];
   preg_match_all("/\d+/", $order, $matches);
   if(count(array_count_values($matches[0])) == 12) {
      // All are unique values
      echo 'All conditions met';
   }
}else{
   echo 'Conditions not met';
}

Upvotes: 1

Views: 630

Answers (3)

axiac
axiac

Reputation: 72206

Assuming the input string is valid (all conditions are met) when it contains in item[] all the values from 1 to 12, this simple piece of code works faster than preg_match() and it's easier to understand:

// Input string
$order = "item[]=2&item[]=1&item[]=3&item[]=4&item[]=5&item[]=6&item[]=7&item[]=8&item[]=9&item[]=10&item[]=11&item[]=12";

// Parse it to values and store them in $pieces
$pieces = array();
parse_str($order, $pieces);

// Need to sort the values to let the comparison succeed
sort($pieces['item']);
$valid = ($pieces['item'] == range(1, 12));

// Verification
var_dump($valid);
// It prints:
// bool(true)

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

The right way would be using
parse_str(to parse quesry string: key/value pairs separated with &)
and array_diff(to check if all numbers from the needed range 1-12 are present and not repeated) functions:

$order = "item[]=2&item[]=1&item[]=3&item[]=4&item[]=5&item[]=6&item[]=7&item[]=8&item[]=9&item[]=10&item[]=11&item[]=12";
parse_str($order, $items);

if (isset($items['item']) && is_array($items['item'])
    && count($items['item']) == 12 && !array_diff(range(1, 12), $items['item'])) {
    echo 'All conditions met';
} else {
    echo 'Conditions not met';
}

Upvotes: 1

Ataur Rahman
Ataur Rahman

Reputation: 1791

Try this:

<?php

$order = "item[]=2&item[]=1&item[]=3&item[]=4&item[]=5&item[]=6&item[]=7&item[]=8&item[]=9&item[]=10&item[]=11&item[]=12";
$pattern = "/^(item\[\]=([1-9]|10|11|12))(&(item\[\]=([1-9]|10|11|12))){11}$/";

if(preg_match($pattern, $order)) {

   // check for repetition
   $matches = [];
   preg_match_all("/\d+/", $order, $matches);
   if(count(array_count_values($matches[0])) == $movienumber) {
       // All are unique values
       echo 'All conditions met';
    }
}else{
   echo 'Conditions not met';
}

You were missing a ) in pattern.

Upvotes: 0

Related Questions