How to extract numbers by preserving their formatting of a PHP string?

Asked

Viewed 93 times

2

I’m having a question, how can I do to extract numbers from a PHP string preserving the formatting of it?

I found a solution in the forum in English, but when extracting the numbers the pattern of the date, for example, is not preserved.

$str = 'Paga. ref. a nota fiscal número 8888, com o cheque número 9999 em 14/08/2015';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);

Source: Extract Numbers from a string

1 answer

2


$re = "/([0-9\\/])/"; 
$str = "Paga. ref. a nota fiscal número 8888, com o cheque número 9999 em 14/08/2015"; 

preg_match_all($re, $str, $matches);

Check in the Regex. https://regex101.com/r/tV7eY9/1

Explaining

0-9 - Within the string I just want numbers.

\ - Escape bar, otherwise the command will give error when placing the date bar.

/ - In addition to the numbers, I want the bar to be selected as well. But it can be:

- trait

_ underscore

. dots

  • 1

    Thank you very much, this was the very solution I was looking for!

  • That, I had edited by putting these remarks in the code itself. Great, serves for future user questions, because I found that there was nothing similar in the forum to PHP

Browser other questions tagged

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