Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split camelCase word into words with php preg_match (Regular Expression)

How would I go about splitting the word:

oneTwoThreeFour 

into an array so that I can get:

one Two Three Four 

with preg_match ?

I tired this but it just gives the whole word

$words = preg_match("/[a-zA-Z]*(?:[a-z][a-zA-Z]*[A-Z]|[A-Z][a-zA-Z]*[a-z])[a-zA-Z]*\b/", $string, $matches)`; 
like image 391
CodeChap Avatar asked Dec 23 '10 14:12

CodeChap


1 Answers

You can use preg_split as:

$arr = preg_split('/(?=[A-Z])/',$str); 

See it

I'm basically splitting the input string just before the uppercase letter. The regex used (?=[A-Z]) matches the point just before a uppercase letter.

like image 169
codaddict Avatar answered Oct 10 '22 21:10

codaddict