0

I need to know if there is any way I can do the following using regular expressions (or otherwise) in PHP

aaBaa BBB -> aa Baa BBB

ie, I want to introduce a space before a capital letter only if a Capital letter occurs before and after a small letter.

The best I could come up with was something like this

$string = preg_replace('/(\w+)([A-Z])/U', '\\1 \\2', $string);

but that would only give me something like

aaBaa BBB -> aa Baa B B B

Thanks in advance.

2 Answers 2

2

Try this:

preg_replace('|([a-z])([A-Z])([a-z])|', '$1 $2$3', $txt);
Sign up to request clarification or add additional context in comments.

1 Comment

I thank you for your answer, but I felt @sergei's was better as it taught me how to take care of such things in the future.
1

Here: http://rubular.com/r/3xqbuWuiLD

([a-z]+)([A-Z]+)

Comments

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.