PHP - remove DOT from text but exclude numbers

I want to remove the dots ( .) in a line containing numbersand text.

Example: from S.D.M.StoSDMS

But I want to leave ( .) as numbersit is.

Ex: 123.50to123.50

I tried str_replace(), but it deleted all ( .) s

How can I do this in PHP?

+4
source share
1 answer

Usage preg_replace:

$result = preg_replace('/(?<!\d)\.(?!\d)/', '', $string);

Where

  • (?<!\d) - negative lookbehind, assumes that there is no digit before the point.
  • (?!\d) - a negative result, assumes that there is no digit after the point.
+6
source

Source: https://habr.com/ru/post/1664763/


All Articles