8

I'm trying to preg_replace all but Digits and . and , symbols. From a string,

can be

nksdfojsdfjdsfojdsjofsojdfjo213-9sdfknmcfsjozx -xdcv-cv,1239103

Output using preg_replace or any other solution would return just the numbers and allow for comma and dot signs.

To end up with a1,322.44 as 1,322.44

This is what I tried

$carPrice=preg_replace('/\D/', '', $carPrice); 

Would write three more paragraphs if it is necessary, don't seem to find the answer.

lbennet
  • 1,083
  • 5
  • 14
  • 31

2 Answers2

20

Use [^0-9\.,] as your matching expression.

  • thank you very much, interesting in this case you decided to answer and not to vote down on the question – lbennet Feb 12 '13 at 23:27
  • 1
    You updated the question just fine. Asking questions takes a bit of practice, just like everything else. I prefer to answer rather than comment in such situations. –  Feb 12 '13 at 23:28
  • Escaping dots inside of a character class is not necessary. Explaining every answer that you post is necessary. – mickmackusa Sep 25 '20 at 03:29
7

you can use this:

preg_replace('#[^0-9\.,]#', '', 'your value');
Aram Raswll
  • 93
  • 1
  • 5
  • 2
    From Review: Hi, please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Oct 23 '18 at 07:05