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;
1

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

3 Comments

Hi thanks. There is just one issue. There's a space between the . P.
The output I get is like I mentioned in the answer, make sure the code is the same
sorry you are correct, I was trying $string = "A. J. P. Jane B. V."; So I have another aspect to solve.
0

Try this,

$string = preg_replace('/\s+/', '', $string);

3 Comments

this is not the result he wanted!!
That removed all space. It should only remove some. the result should be A.J.P. Jane B.V.
see Alex Andreis answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.