Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Remove DOTs from text but exclude numbers [duplicate]

Tags:

regex

php

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?

like image 865
A B Catella Avatar asked Mar 11 '23 07:03

A B Catella


1 Answers

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.
like image 175
Toto Avatar answered Mar 20 '23 21:03

Toto