Regular expression that validates PHP

2

I am making a regular expression that validates the Venezuelan identity card and I achieve it

if(preg_match_all("/(\d*[0-9]{2})/", $cedula, $resultado)){

    print_r($resultado);
} else {

    echo "No hubo resultado";
}

The problem is that I want the user to enter the values in this way:

$cedula = 18.842.389;

that is, I want my regular expression to take into account the. that the user enters

    
asked by JOSE HERRADA 20.05.2018 в 23:07
source

1 answer

1

Your regular expression should look like this

<?php
$cedula = "18.842.389";

if(preg_match_all("/(\.d*[0-9]{2})/", $cedula, $resultado)){

    print_r($resultado);
} else {

    echo "No hubo resultado";
}
  

As notes, before the letter d and then from 0 to 9 it will verify that   just numbers we add the point; you do the test and with point   shows the array with the positions and if you remove them or put another   thing like middle guion shows you There was no result

But I need you to be with any special character

So instead of the point symbol you put \ W but it's capitalized and with that you validate that you enter between the figures special characters look

<?php


$cedula = "18.842.389";

if(preg_match_all("/(\W\d*[0-9]{2})/", $cedula, $resultado)){

   print_r($resultado);
} else {

    echo "No hubo resultado";
}

I leave you a list for special characters:

  


$ Match at the end of the string
\ s Match with any   White space
\ d Match with any digit
\ D Match   with any character other than a digit
\ w Match with   any character that can be part of a word (letter, number,   low script)
\ W Match with any character that CAN NOT be   part of a word (letter, number, underscore)
\ A Start of a   string.
\ z End of a string.

    
answered by 21.05.2018 / 00:38
source