Split a String On Capital Letters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Split a String On Capital Letters

Post by N1gel »

Hi

I'm trying to split a sting on capital letters. I'm sure I could do it with a loop through each character and gettting the ASCII value but I was thinking there might be a simple way to do it with preg_split. However I'm not very experienced with Regular Expressions.

Can anybody help?

As an example I'm trying to split the Text "RegularExpression" to an array like Array([0]=>Regular [1]=>Expression)

Code: Select all

print_r(preg_split("[A-Z]","RegularExpression"));
Also If anybody knows of a good place for notes on Regular Expressions that would be great

Thanks

Nigel
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Split a String On Capital Letters

Post by mikosiko »

http://www.addedbytes.com/cheat-sheets/ ... version-1/

and be sure to read version 2 too....

you can try also this approach:

Code: Select all

$astr = preg_split('/([A-Z])/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($astr);
NOTE: Edited before see AbraCadaver's post....
Last edited by mikosiko on Mon May 10, 2010 11:05 am, edited 2 times in total.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Split a String On Capital Letters

Post by AbraCadaver »

If you add delimiters to your expression "/[A-Z]/" it will work, unfortunately you lose the capital letters. preg_split() won't work here, but you can return an array of matches like:

Code: Select all

preg_match_all("/[A-Z][^A-Z]*/", "RegularExpression", $matches);
print_r($matches);
This may need to be tweaked depending on how you want to handle spaces, numbers, consecutive caps etc...

I use: http://www.regular-expressions.info
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Split a String On Capital Letters

Post by AbraCadaver »

mikosiko wrote:http://www.addedbytes.com/cheat-sheets/ ... version-1/

and be sure to read version 2 too....

you can try also this approach:

Code: Select all

$astr = preg_split('/([A-Z])/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($astr);
NOTE: Edited before see AbraCadaver's post....
As I said, preg_split() doesn't work as needed:
[text]Array
(
[0] =>
[1] => R
[2] => egular
[3] => E
[4] => xpression
)[/text]
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Split a String On Capital Letters

Post by mikosiko »

NOTE: Edited before see AbraCadaver's post....
I was agreeing with you :wink:
Post Reply