Press "Enter" to skip to content

函数驼峰命名和下划线命名转换

Last updated on 2019-05-16

<?php
//下划线命名法改变成驼峰命名法
function aa_bb($a)
{
    return str_replace(' ', '', ucwords(str_replace('_', ' ', $a)));
}

echo aa_bb('aa_bb_cc');
//驼峰命名法改变成下划线命名法(一、效率低 不推荐)
function CcDd($aa)
{
    $b = '';
    for ($i = 0; $i < strlen($aa); $i++) {
        if (ord($aa[$i]) >= 65 && ord($aa[$i]) <= 90) {
            $b .= "_" . strtolower($aa[$i]);
        } else {
            $b .= $aa[$i];
        }
    }
//var_dump($b);

    $b = ltrim($b, '_');

    return $b;
}

echo CcDd('CcDdEe');
//驼峰命名法改变成下划线命名法(二、推荐)
$val = 'AaBbCc';
echo trim($val, 'he');
echo ltrim(preg_replace_callback(
    '/[A-Z]/',
    function ($match) {
        return "_" . strtolower($match[0]);
    }, $val), '_');

 

Leave a Reply

Your email address will not be published. Required fields are marked *