-7

I want to replace all dots in a string to commas, but only for numbers (in PHP):

Like :

this is. an example. 15.68€

and it should be converted to something like this is. an example. 15,68€

Is there an easy preg_replace or any other option ?

devios1
  • 36,899
  • 45
  • 162
  • 260
  • Depends on what you call “easy” … Simply demanding a digit before and after the dot, that would be pretty easy. Have you _tried_ anything yet? – 04FS Apr 18 '19 at 09:22
  • Have you tried [number_format()](https://www.php.net/manual/en/function.number-format.php) before adding the currency sign ? – B001ᛦ Apr 18 '19 at 09:23
  • @04FS But that would also replace the dots in, say, filenames that would match this pattern. – xlecoustillier Apr 18 '19 at 09:24
  • This is a simple matter of number formatting/localization, but that implies that you have your number as a standalone value at some point. Trying to find/replace that in an existing string is not the best solution IMHO. – xlecoustillier Apr 18 '19 at 09:25
  • _that would also replace the dots..._ I thought maybe they have standalone values for the numbers :) @xlecoustillier – B001ᛦ Apr 18 '19 at 09:25
  • @xlecoustillier so what, if OP doesn’t want that, they would have to be more specific with their requirements to begin with. – 04FS Apr 18 '19 at 09:28
  • @04FS Requirements are clear : replace dots with commas in strings _only for numbers_ :) – xlecoustillier Apr 18 '19 at 09:30
  • @xlecoustillier so what do you do with an input text like `The name of the file in question is 'we paid 15.68€ for this.txt'` then? :-p – 04FS Apr 18 '19 at 09:34
  • 1
    @04FS Nice one :) But in this case this is not a number, this is a filename, so don't do anything (unless you want dead references in your app). – xlecoustillier Apr 18 '19 at 09:39
  • @xlecoustillier _“But in this case this is not a number, this is a filename,”_ - but how would you determine that in a regular expression? Want to go by the fact that it has a `.txt` suffix/ any dot-someletters suffix? Great, then I give you the same example, but with a folder name, so no `.txt` at the end. Now what? – 04FS Apr 18 '19 at 09:42
  • 1
    @04FS That's why I suggested that trying to do that directly in strings was not the best idea. That doesn't make the requirement less clear, but it clearly shows that the current approach will necessarily generate false positives in the long run. – xlecoustillier Apr 18 '19 at 09:44
  • @xlecoustillier yeah I know, but I guess we can assume OP only has the data available in this format to begin with. – 04FS Apr 18 '19 at 09:47

1 Answers1

1

Try this one

  $str = "this is. an example. 15.68€";
  function cleaner($matches)
   {
     return str_replace("." , "," , $matches["nrs"]);
   }

 $str = preg_replace_callback ("/(?P<nrs>[0-9]+.[0-9]+)/" , "cleaner" , $str);
 echo $str;

I have used preg_replace_callback. The behavior of this function is almost identical to preg_replace(), except for the fact that instead of replacement parameter, one should specify a callback. Check for more about it here. You can also test the live preview of my solution.

Zain Farooq
  • 2,956
  • 3
  • 20
  • 42