32 Answers 32 Sorted by:
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending. 1
A shorter solution: Similar to the editors one with a simplified regular expression and fixing the “trailing-underscore” problem:
Note that cases like
SimpleXML will be converted to
simple_x_m_l using the above solution. That can also be considered a wrong usage of camel case notation (correct would be
SimpleXml) rather than a bug of the algorithm since such cases are always ambiguous – even by grouping uppercase characters to one string (
simple_xml) such algorithm will always fail in other edge cases like
XMLHTMLConverter or one-letter words near abbreviations, etc. If you dont mind about the (rather rare) edge cases and want to handle
SimpleXML correctly, you can use a little more complex solution:
Try this on for size:
This implements the following rules:
- one or more uppercase letters and digits (followed by either the end of the string or an uppercase letter followed by a lowercase letter or digit ie the start of the next sequence); or
- one or more lowercase letters or digits.
A concise solution and can handle some tricky use cases:
Can handle all these cases:
You can test this function here: http://syframework.alwaysdata.net/decamelize
Ported from Rubys
One trick the above solutions may have missed is the e modifier which causes
preg_replace to evaluate the replacement string as PHP code.
Most solutions here feel heavy handed. Heres what I use:
lcfirst($camelCase)will lower the first character (avoids CamelCASE converted output to start with an underscore)
[A-Z]finds capital letters
+will treat every consecutive uppercase as a word (avoids CamelCASE to be converted to camel_C_A_S_E)
strtolower([…])turns the output to lowercases
php does not offer a built in function for this afaik, but here is what I use
the splitter can be specified in the function call, so you can call it like so
“CamelCase” to “camel_case”:
You need to run a regex through it that matches every uppercase letter except if it is in the beginning and replace it with underscrore plus that letter. An utf-8 solution is this:
If you are not sure what case your string is, better to check it first, because this code assumes that the input is
camelCase instead of
dash-Case, so if the latters have uppercase letters, it will add underscores to them.
The accepted answer from cletus is way too overcomplicated imho and it works only with latin characters. I find it a really bad solution and wonder why it was accepted at all. Converting
test123_string is not necessarily a valid requirement. I rather kept it simple and separated
a_b_cccc instead of
ab_cccc because it does not lose information this way and the backward conversion will give the exact same string we started with. Even if you want to do it the other way it is relative easy to write a regex for it with positive lookbehind
(? or two regexes without lookbehind if you are not a regex expert. There is no need to split it up into substrings not to mention deciding between
lcfirst where using just
strtolower would be completely fine.
If you are looking for a PHP 5.4 version and later answer here is the code:
I had a similar problem but couldnt find any answer that satisfies how to convert CamelCase to snake_case, while avoiding duplicate or redundant underscores
_ for names with underscores, or all caps abbreviations.
Th problem is as follows:
The solution I wrote is a simple two functions call, lowercase and search and replace for consecutive lowercase-uppercase letters:
Not fancy at all but simple and speedy as hell:
A version that doesnt use regex can be found in the Alchitect source:
So here is a one-liner:
danielstjules/Stringy provieds a method to convert string from camelcase to snakecase.
Laravel 5.6 provides a very simple way of doing this:
What it does: if it sees that there is at least one capital letter in the given string, it uses a positive lookahead to search for any character (
.) followed by a capital letter (
(?=[A-Z])). It then replaces the found character with its value followed by the separactor
The direct port from rails (minus their special handling for :: or acronyms) would be
Knowing PHP, this will be faster than the manual parsing thats happening in other answers given here. The disadvantage is that you dont get to chose what to use as a separator between words, but that wasnt part of the question.
Also check the relevant rails source code
Note that this is intended for use with ASCII identifiers. If you need to do this with characters outside of the ASCII range, use the /u modifier for
Here is my contribution to a six-year-old question with god knows how many answers...
It will convert all words in the provided string that are in camelcase to snakecase. For example "SuperSpecialAwesome and also FizBuzz καιΚάτιΑκόμα" will be converted to "super_special_awesome and also fizz_buzz και_κάτι_ακόμα".
Yii2 have the different function to make the word snake_case from CamelCase.
If you are not using Composer for PHP you are wasting your time.
Use Symfony String
The worst answer on here was so close to being the best(use a framework). NO DONT, just take a look at the source code. seeing what a well established framework uses would be a far more reliable approach(tried and tested). The Zend framework has some word filters which fit your needs. Source.
here is a couple of methods I adapted from the source.
There is a library providing this functionality:
If you use Laravel framework, you can use just snake_case() method.
This is one of shorter ways:
How to de-camelize without using regex:
How would I do that in 2019:
PHP 7.3 and before:
And with PHP 7.4+:
Its easy using the Filter classes of the Zend Word Filters:
The open source TurboCommons library contains a general purpose formatCase() method inside the StringUtils class, which lets you convert a string to lots of common case formats, like CamelCase, UpperCamelCase, LowerCamelCase, snake_case, Title Case, and many more.
To use it, import the phar file to your project and:
Thanks for contributing an answer to Stack Overflow!
To learn more, see our tips on writing great answers. Draft saved Draft discarded
This depends on the programmers/developers as well as the framework or open source they are working on. For example, in the Yii framework, it is advisable to use camel case whereas in the Codeigniter framework, most people follow variable names with underscores. One thing for certain is that regardless of which framework you use, the variable name must be understandable and identifiable.
So, in a way you can say, by sticking to stubborn conventions to use camel case exclusively, your insistence on "consistency" where no rules apply actually breaks consistency in other places.
Of course, you could just break with the convention to capitalize acronyms, writing "Html" instead of "HTML", but what if a word ending in "l" is followed by a word starting with "I". There are some situations like that, and it always tends to slow down my coding, since I spend more time trying to come up with alternative names for my functions.
In practice, we are trying to add functionality that should be part of the filesystem. Ideally, you could just add "tags" to each file individually, and it would somehow display the tag when browsing in your editor or file manager. Personally I dislike having "abstract" or other keywords in my filenames, because it restricts my ability to name the class internally because of autoloading.
Besides that, it is also an extremely weird obsession some developers have, because it plainly does not matter what you use. You can even mix underscores (snake case) with camel case if you want – editors will suggest and autocomplete just fine regardless. The obsession is so big that it has even been "standardized" in PSR-1 by PHP-FIG.
PSR-1 does not permit underscores for method names, but it probably should. Also, class names most use StudlyCaps.
Does PHP use CamelCase?
How can I get first letter capital in PHP?
The ucfirst() function converts the first character of a string to uppercase. Related functions: lcfirst() - converts the first character of a string to lowercase. ucwords() - converts the first character of each word in a string to uppercase.
How do you lowercase in PHP?