views:

48

answers:

2

I want to remove spaces from strings where the space is preceeded by a digit or a "." and acceded by a digit or ".". I have strings like: "50 .10", "50 . 10", "50. 10" and I want them all to become "50.10" but with an unknown number of digits on either side. I'm trying with lookahead/lookbehind assertions like this:

$row = str_replace("/(?<=[0-9]+$)\s*[.]\s*(?=[0-9]+$)/", "", $row);

But it does not work...

+2  A: 

Maybe a simple

$row = preg_replace('#(\d+)\s*\.\s*(\d+)#', '$1.$2', $row);

could suffice?

kemp
A: 
$str = '50 .10, 50 . 10, 50. 10';
$str = preg_replace('/(\d+)\s*\.\s*(\d+)/', '$1.$2', $str);
echo($str);  // results in "50.10, 50.10, 50.10"
Rob