Solved

Convert CamelCase (or Proper Case) to Underscore lower case

Posted on 2008-06-15
1
4,081 Views
Last Modified: 2011-09-20
I need to convert a CamelCase (or ProperCase)  string into a underscored lower case string.

Bascially, I need to take something like this:

test_ThisIsATest;

and make a new string come out like this:
test_this_is_a_test;

Anyone know of any easy way to do this in PHP?

Thanks!
0
Comment
Question by:swhitlow
1 Comment
 

Accepted Solution

by:
swhitlow earned 0 total points
ID: 21790139
I ended up working this one out myself. Here is the function I used just in case anyone else might need it:

function underscore($word)
          {
              static $_cached;
              if(!isset($_cached[$word])){
                  $_cached[$word] = strtolower(preg_replace(
                  array('/[^A-Z^a-z^0-9^\/]+/','/([a-z\d])([A-Z])/','/([A-Z]+)([A-Z][a-z])/'),
                  array('_','\1_\2','\1_\2'), $word));
              }
              return $_cached[$word];
          }
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question