London Smith
London Smith

Reputation: 1659

Keep a character found by preg_replace

I want to replace a dot followed by a space followed by a uppercase and I tried this pattern to replace it by    :

preg_replace('/\. [A-Z]/', '.    'With marriage came a move to the beautiful Birmingham, Alabama area. Diving in head first.');

It's working but I am loosing the D of Diving in head first.

How can I keep it?

Upvotes: 0

Views: 36

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626754

Put the letter matching pattern into a non-consuming lookahead:

'/\.\s+(?=[A-Z])/'

The \s+ will match 1 or more whitespaces (or if you do not want that, keep the regular space) and (?=[A-Z]) makes the engine require an uppercase ASCII letter to appear right after the current location in the string.

See the PHP demo printing With marriage came a move to the beautiful Birmingham, Alabama area.   Diving in head first..

Upvotes: 1

Related Questions