Filter numbers in PHP

Asked

Viewed 123 times

0

I have the following phone in the database: (45) 9 9874-4700

Is there any function that I leave only NUMBERS? regardless of which character is in the middle of the tag?

1 answer

3


Yes. You can use the function preg_replace. It will use regular expressions to remove, change certain characters.

Example:

preg_replace("/\D/", "", "(45) 9 9874-4700");

In regular expressions, the \D means: All characters different from 0 to 9.

That is, we select all characters that are not numbers and in the second parameter we inform which characters to exchange (in this case for none).

https://regex101.com/r/QnVpJP/1

Browser other questions tagged

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