4

I want remove dots (.) in a string containing numbers and text.

Ex: S.D.M.S to SDMS

But I want to leave the (.) in numbers as it is.

Ex: 123.50 to 123.50

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

How can I do it in PHP?

A B Catella
  • 308
  • 4
  • 12

1 Answers1

7

Using preg_replace:

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

where

  • (?<!\d) is a negative lookbehind, assumes there's no digit before the dot.
  • (?!\d) is a negative lookahead, assumes there's no digit after the dot.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Toto
  • 89,455
  • 62
  • 89
  • 125