Paste string by format

1

I have an array with 1000 indexes with random text and random numbers, so I need to get in this text excerpts that are in particular formats containing pre-defined character quantities, such as a string as follows: 65 45 98 12 15 98 17 characters) or 04668475 03/1980 (date format after numeric sequence), how can I create a filter function to pass the indexes through it and just return the strings on the screen that fit those formats?

Informal example:

$strings = array("1" => "12 32 87 98 54", "2" => "154654651", "3" => "1354654654  45 45 45 45 45");
$mascara = ## ## ## ## ##;
$contar = count($strings);

for($i=0; $i < $contar; $i++){

    if($mascara == $strings[$i]){
        echo $strings[$i]."encontrado";
    }else{
        echo $strings[$i]."nao encontrado";
    }

}

In case the indexes would be compared to the mask and if there is an excerpt of the string with the format of the mask would be printed as found.

    
asked by anonymous 12.01.2018 / 07:51

1 answer

0

As commented , you will have only two formats allowed : "0000 0000 0000 0000 00/0000 000" and "0000000000000000 00/0000 000", that is, a sequence of 16 digits, can be grouped every four, followed by a date and a sequence of three digits. >

So we can define a regular expression:

((\d{4}\s?){3}\d{4}) (\d{2}\/\d{4}) (\d{3})

Where:

  • ((\d{4}\s?){3}\d{4}) will capture the 16-digit sequence, and there may or may not be spaces every four digits. Read: four digits, \d{4} , followed by an optional blank, \s? , repeated three times, {3} , followed by four digits, \d{4} , in parentheses, to capture the value; li>
  • A blank space;
  • (\d{2}\/\d{4}) will capture the date: two digits followed by four digits;
  • Other space;
  • (\d{3}) will capture any three-digit number;

If you want to validate a text by checking whether it follows this format strictly, you can start the expression with ^ and end with $ , which define the beginning and end of the text, so if the text has , or not, anything that fades from the format the filter will block.

^((\d{4}\s?){3}\d{4}) (\d{2}\/\d{4}) (\d{3})$

See a simple example:

$tests = [
  "0000 0000 0000 0000 00/0000 000",   // válido
  "0000000000000000 00/0000 000",      // válido
  "00000000 00000000 00/0000 000",     // válido
  "0000000 000000000 00/0000 000",     // inválido: espaços errados
  "0000 000 0000 0000 00/0000 000",    // inválido: tamanho da sequência errada
  "000000000000000 00/0000 000",       // inválido: tamanho da sequência errada
];

foreach($tests as $test)
{
  if (preg_match("/^((\d{4}\s?){3}\d{4}) (\d{2}\/\d{4}) (\d{3})$/", $test, $matches))
  {
    print_r($matches);
  }
}

See working at Ideone | Repl.it

    
12.01.2018 / 23:39