Skip to content

Instantly share code, notes, and snippets.

@Exadra37
Created February 12, 2014 16:58
Show Gist options
  • Save Exadra37/8959693 to your computer and use it in GitHub Desktop.
Save Exadra37/8959693 to your computer and use it in GitHub Desktop.
some usefull php methods
<?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