Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php explode at capital letters?

Tags:

function

php

I have strings like:

$a = 'helloMister'; $b = 'doggyWaltz'; $c = 'bumWipe'; $d = 'pinkNips'; 

How can I explode at the capital letters? I have search on google for some time and came back with nothing!

like image 245
cgwebprojects Avatar asked Jan 25 '12 05:01

cgwebprojects


People also ask

What does explode () do in PHP?

The explode() function breaks a string into an array. Note: The "separator" parameter cannot be an empty string. Note: This function is binary-safe.

How can I capital a word in PHP?

The strtoupper() function converts a string to uppercase. Note: This function is binary-safe. Related functions: strtolower() - converts a string to lowercase.


1 Answers

If you want to split helloMister into hello and Mister you can use preg_split to split the string at a point just before the uppercase letter by using positive lookahead assertion:

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

and if you want to split it as hello and ister you can do:

$pieces = preg_split('/[A-Z]/',$str); 
like image 91
codaddict Avatar answered Sep 20 '22 23:09

codaddict