Created
February 12, 2014 16:58
-
-
Save Exadra37/8959693 to your computer and use it in GitHub Desktop.
some usefull php methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Converts a CamelCase word into snake_case | |
* - improved version of http://stackoverflow.com/a/19533226 | |
* - this version avoid failing when using the $input = "_ThisIsATest_To_check_this" | |
* . stack overlow answer will output "__this_is_atest__to_check_this" | |
* . this improved version will output "this_is_a_test_to_check_this" | |
* | |
* @author Exadra37 exadra37 in gmail point com | |
* | |
* @version 1.0.0 | |
* | |
* @since 12/02/2014 - v.1.0.0 | |
* | |
* @param string $input - string to convert to snake_case | |
* | |
* @return string - string converted to snake_case | |
*/ | |
public function snakeCase($input, $splitter = "_") | |
{ | |
return ctype_lower($input) ? $input : str_replace(array("-", "_", "{$splitter}{$splitter}"), $splitter,ltrim(strtolower(preg_replace("/[A-Z]/", "{$splitter}$0", $input)), $splitter)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment