1

I have a string in format:

Rooms available: Master bedroomDining roomLiving room

I want to convert this string into like:

Rooms available: Master bedroom, Dining room, Living room

So, inserting a ', ' (comma and space) in between each lowercase_char[here]UPPERCASE_CHAR

Getting the lowercase_charUPPERCASE_CHAR is easy but can't figure out how to insert ', ' in between them.

Anyone?

1 Answer 1

3

If you are using a regex, you need to put the each letter in its own capture group, so you can split them apart.

$string = 'Rooms available: Master bedroomDining roomLiving room';

$string = preg_replace('/([a-z])([A-Z])/', '$1, $2', $string);

DEMO: https://regex101.com/r/vQ6hP7/1

Sign up to request clarification or add additional context in comments.

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.