Rule PREG MATCH appropriate

2

I created a loop that shows all the months of the year with the following code:

for($i = 1;$i <= 12;$i++){

}

Then I created a $ _GET variable that would receive a rule like the following:
1,2,3,4,5,6,7,8,9,10,11,12
This is the $ _GET variable:

$listaMesesPagos = isset($_GET["mesespg"]) ? $_GET["mesespg"] : null;$listaMesesPagos = is_string($listaMesesPagos) ? $listaMesesPagos : null;


The purpose of this $ _GET variable is to show the months that have been paid by person X. For example if person X has paid their monthly fee in the months: January, February and March , 2,3). Then $ _GET would get the values: 1,2 and 3 in the following way: link that would later be thrown into an ARRAY php by explode ()

$arrayMeses = explode(",",$listaMesesPagos);

But to use explode () it is first necessary to know if the variable $ _GET received in the following rule: n, n, n that in this example is : 1,2,3 so I used preg_match with the following $ pattern:

$regraDaListaDosMeses = '/^([0-9]{1,2}\,{1}){1,11}([0-9]){0,1}$/';


Thus, before using the explode, php checks if the variable $ _GET is in this rule above: (1,2,3). If it is, PHP will throw the numbers into an array similar to this: array (1,2,3); otherwise php will create an empty array to avoid errors as it is in the following code:

if(preg_match($regraDaListaDosMeses, $listaMesesPagos) == true){
$arrayMeses = explode(",",$listaMesesPagos);
}else{
    $arrayMeses = array();
}

Then the for loop would check if the current month is in the array $ arrayMeses if it would not show that the month is pending. Here is the code:

            for($i = 1;$i <= 12;$i++){
            /*mês pago*/if(in_array($i, $arrayMeses)){/*CÓDIGO AQUI*/}
            /*mês não pago*/else{/*CÓDIGO AQUI*/}
        }

Now, based on this " delicate " explanation. :) Let's go to the problem that is in the preg_match rule. see, in the rule: / ^ ([0-9] {1,2} \, {1}) {1,11} ([0-9]) {0,1} $ / . This is not appropriate. I want a rule that verifies that, for example, the following characters are correct: 1,2,3 , 1,2,3, ; 1,2 , 3,4,5,6,7,8,9,10,11,12 .
Can you help me?

    
asked by anonymous 20.11.2017 / 17:27

1 answer

1

A number integer between 1 and 12:

[1-9]|1[0-2]

Grouped :

(?:[1-9]|1[0-2])


Followed by a comma or the end of the text ( $ ):

(?:[1-9]|1[0-2])(?:,|$)


Repeat 0 to 12 times:

(?:(?:[1-9]|1[0-2])(?:,|$)){0,12}


From the beginning ( ^ ) to the end of the text ( $ ):

^(?:(?:[1-9]|1[0-2])(?:,|$)){0,12}$


Then:

$regraDaListaDosMeses = '/^(?:(?:[1-9]|1[0-2])(?:,|$)){0,12}$/';


Demo no regex101.com


Alternatively , you can filter only valid months in the array using preg_grep() :

$listaMesesPagos = '1,@@@,4,,7,128,9,ooo10,11,';
$arrayMeses = explode( ',', $listaMesesPagos);

$regraMes = '/^(?:[1-9]|1[0-2])$/';

$arrayMeses = preg_grep( $regraMes, $arrayMeses);
var_export($arrayMeses);

Result:

array (
  0 => '1',
  2 => '4',
  4 => '7',
  6 => '9',
  8 => '11',
)


Or without regular expressions, using array_filter() .

    
21.11.2017 / 07:11