Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP line break where lower-case letters meets uppercase-letter

I have a string, that is like the following:

$string = "New video gameSome TV showAnother item";

I want to be able to access each item individually, to get something like the following output:

New video game
Some TV show
Another item

How can I add a \n after each item name in the string or some other random character, that I can later explode into an array to access each item individually in the string?

like image 763
user3430837 Avatar asked Mar 18 '14 05:03

user3430837


1 Answers

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

To answer your comment to include words that end in a closing parenthesis or a number:

$string = preg_replace('/([a-z0-9\)])([A-Z])/', "\\1\n\\2", $string);
like image 97
Dave Meybohm Avatar answered Oct 23 '22 05:10

Dave Meybohm