163

I'm trying to remove everything from a string but just numbers (0-9).

I thought this would work..

echo preg_replace("[^0-9]","",'604-619-5135');

But it echos "604-619-5135". What am I missing???

2

4 Answers 4

336

Try this:

preg_replace('/[^0-9]/', '', '604-619-5135');

preg_replace uses PCREs which generally start and end with a /.

Sign up to request clarification or add additional context in comments.

4 Comments

The inner/double quoting doesn't have anything to do with PCRE tho. Apparently (in the days before (?flags)) the people who designed/wrote the function/API thought it was a good idea to pass the regex flags with the double quoted /flags form instead of using an extra function parameter.
@Qtax: good point, yeah I know that's where we get the word "grep" from ("g/re/p")
adding character at the las part seems doesnt remove the part
Or just do it simple and use /\D/ to get the number or /\d/ to get the string.
145

This is for future developers, you can also try this. Simple too

echo preg_replace('/\D/', '', '604-619-5135');

4 Comments

I have come back 2 the future and I can confirm they have still not found a better way.
@Eoin to make this pattern "better" add a one-or-more quantifier (+) after the D. This allows the regex engine to make longer and therefore fewer replacements. Imagine standing in front of a carton of eggs. If I asked you to pick up the dozen eggs, would you do 12 squats or just 1? Though I like to work out, I'd squat once and pick up all of the eggs as a matter of directness.
@mickmackusa, would you add another comment with such code, please 🙏?
@Jos echo preg_replace('/\D+/', '', '604-619-5135');
15

You would need to enclose the pattern in a delimiter - typically a slash (/) is used. Try this:

echo preg_replace("/[^0-9]/","",'604-619-5135');

1 Comment

8

NOTE: if you are filtering strings containing - sign is not applicable to this answer because - stands for minus sign and is NOT filtered and removed. (like -2, -4 etc). If you still want to use it please remove - characters and then try.

(- + . e) characters are not stripped with this function

a much more practical way for those who do not want to use regex:

$data = filter_var($data, FILTER_SANITIZE_NUMBER_INT);

note: it works with phone numbers too.

5 Comments

FILTER_SANITIZE_NUMBER_INT leaves plus + and minus - signs, not only digits.
yes I said it works with phone numbers like +905360000000 leaves out all other characters
This is e.g. a problem if you try to use it with Whatsapp as WA DOES NOT allow + signs!
This answer is completely ineffective for the question asked. Proof: 3v4l.org/IMQeJ
if you read manual page for this you can see that your proof contains - character which is allowed for int if you use -2 for example. so you cannot apply this. you can use $str = str_replace("-", "", $str); and apply this filter_var.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.