SnakeCase CamelCase 互轉

function snakeCaseToCamelCase( string $string, bool $capitalizeFirstCharacter = false ) : string
{

    $str = str_replace('_', '', ucwords($string, '_'));

    if ( ! $capitalizeFirstCharacter )
    {
        $str = lcfirst($str);
    }

    return $str;
}

function camelCaseToSnakeCase( string $string ) : string
{
    return strtolower(preg_replace(['/([a-z\d])([A-Z])/', '/([^_])([A-Z][a-z])/'], '$1_$2', $string));
}

Tags : [ PHP ]