Appropriate PREG MATCH rule

Asked

Viewed 118 times

2

I created a loop that shows all 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 variable $_GET:

$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 were paid by person X. For example if person X paid their monthly fee in the months: January, February and March (1,2,3). Then $_GET would receive the values: 1,2 and 3 as follows: http://localhost/pagina.php? mesespg=1,2,3 that would later be played on a ARRAY php through the explode() being like this:

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


But to use the explode() you first need to know if the $_GET variable received in the following rule: n,n,n that in this example is: 1,2,3 so I used the 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 whether the $_GET variable is in this above rule: (1,2,3). If so, PHP will play the numbers for an array that looks like this: array(1,2,3); otherwise php will create an empty array to avoid errors as in the following code:

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

So the loop for() would check if the current month is in the array $arrayMeses case would not show that the month is pending. Follow 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 that "delicate" explanation. : ) we go to the problem that is in the rule of preg_match.
see, in the rule: /^([0-9]{1,2}\,{1}){1,11}([0-9]){0,1}$/. This is not appropriate. I want a rule that checks 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?

1 answer

1


A number whole amid 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 (^) until the end of the text ($):

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


So:

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


Demo no regex101.com



Alternatively, you can filter only valid months of 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);

Upshot:

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


Or without regular expressions, using array_filter().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.