Hello. I have a price in scv format, where the prices are very crooked: in some places the integer and fractional digits are separated by a dot, in some - a comma, and in some of the numbers there is also some kind of garbage from letters and krakozyabr. I'm trying to replace with preg_replace () (php) all non-numeric garbage to an empty string, and a comma to a period, but in regexp I'm noob and nifiga. Please tell me how to do it right?

  • 3
    Please add examples of lines with which you work and the expected result after processing these lines. So it will be easier to tell you. - ApInvent
  • 2ï¾ 388.66 -> 388.66 - chp

1 answer 1

  1. Replace commas with dots. As a result, there are no more commas.
  2. "[^0-9.]+" - selection of the next portion of non-numeric characters.

team

php -r "echo preg_replace('/[^0-9.]+/', '', 'adasd098xxx.098RRRR');"

will issue:

098.098

  • one
    then so php -r "echo preg_replace('/[^0-9,.]+/', '', str_replace(',','.','adasd098xxx,098RRRR'));" - username
  • Thank! At some point it dawned on me that kryakozyabry is an inseparable space, separating the digits and turned into a something otherworldly when converting into a Microsoft with Exelem .csv. Your example did what you need: 2ï¾ 388.66 -> 2388.66 - chp