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
?
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With