Forum Moderators: coopster

Message Too Old, No Replies

Split String on every UPPERCASE letter. Is This Possible?

For instance: firstName ---> First Name

         

cosmoyoda

8:26 pm on Mar 23, 2008 (gmt 0)

10+ Year Member



I am working with a large database. Most of the compound field names in the tables do not separate words with special characters, they simply use uppercase to distinguish it.

For example: in the USERS table, I have a field called firstName. I want to know I could split firstName into two parts ("first" and "Name") so I can work with it on my tables. I would use those field names for my table headers throughout my site. I was wondering if this is possible.

Once "firstName" is converted into "first Name", I can easily set "first" to be uppercase as well ("First") and the final result would be "First Name", just how I wanted.

Thanks!

eelixduppy

9:05 pm on Mar 23, 2008 (gmt 0)



Something like this should work out nicely:

$str = 'lastName';
#
$pattern = "/(.)([A-Z])/";
$replacement = "\\1 \\2";
echo [url=http://www.php.net/ucfirst]ucfirst[/url]([url=http://www.php.net/preg-replace]preg_replace[/url]($pattern, $replacement, $str));

cosmoyoda

12:01 am on Mar 24, 2008 (gmt 0)

10+ Year Member



Your code worked out perfect, eelixduppy.

Thank you so much. Happy easter!

eelixduppy

12:52 am on Mar 24, 2008 (gmt 0)



Thanks, you have a great Easter, too. Glad everything worked out for you :)