1

I am to remove the spaces between the initials and keep the space between initial and any word.

I would like to get this result. A.J.P. Jane B.V.

Instead I am getting this result A.J. P. Jane B. V.

$string = "A.J. P. Jane B. V.";

$string =  preg_replace('/\.\s\[A-z]{1}\./', '.', $string);

echo $string;
user2635901
  • 203
  • 2
  • 12
  • Possible duplicate of [To strip whitespaces inside a variable in PHP](http://stackoverflow.com/questions/1279774/to-strip-whitespaces-inside-a-variable-in-php) – Eli Stone Jan 18 '16 at 15:31

2 Answers2

2

Use this rule \.\s([A-Z]{1})\. or \.\s([A-Z])\. without explicit limit to match [dot][space][letter][dot] and
replace with .$1., [dot][letter][dot]

$string =  preg_replace('#\.\s([A-Z]{1})\.#', '.$1.', $string);

echo $string;

Will output

A.J.P. Jane B.V.
Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
0

Try this,

$string = preg_replace('/\s+/', '', $string);
monirz
  • 534
  • 5
  • 14