-1

Can you please tell me how to correctly make a regular expression to replace a point with a comm between two digits in a sentence. For example:

Text text 119.20 text. Text text.
to
Text text 119,20 text. Text text.

I found such an example, but this expression incorrectly handles 4-digit numbers.

preg_replace('/([\d]).([\d])/','$1,$2',$example);
Jeick9
  • 15
  • 8
  • it only mishandles 4 digit numers if they are formatted like this: 1,123,133.02 - which is (I think) the us way of using thousands-seperators. Its difficult to get a regex for that - your program is unable to decide if 1,234 is 1234.00 or 1+234/1000 - no regex can distinguish those 2. German would format above number like this: 1.123.133,02. In any case, please provide replacementdata for : `1,123,456.00` `1,234`, `53,123,123` , `1.123,44` - what should each one be replaced with? – Patrick Artner Jan 06 '18 at 23:21
  • In all cases, I just need to replace the point with a coma in those cases, if before and after the point exist a digit in sentence. – Jeick9 Jan 06 '18 at 23:36
  • You must escape the dot, use `'/(\d)\.(\d)/'`. See https://stackoverflow.com/questions/399078/what-special-characters-must-be-escaped-in-regular-expressions – Wiktor Stribiżew Jan 06 '18 at 23:46
  • @WiktorStribiżew That regex will cut the number when it founds it – Art_Code Jan 07 '18 at 00:25
  • Possible duplicate of [What special characters must be escaped in regular expressions?](https://stackoverflow.com/questions/399078/what-special-characters-must-be-escaped-in-regular-expressions) – Wiktor Stribiżew Jan 07 '18 at 00:56

1 Answers1

-1

I've modified your regex to do as you wish:

$test_string='Text text 119.20 text. Text text.';

$regex_numbers=preg_replace('/(\d)\.(\d)/', '\1,\2', $test_string);

echo $regex_numbers;
Art_Code
  • 518
  • 4
  • 12