首页 > 编程 > PHP > 正文

php 下划线和驼峰相互转换

2019-11-14 10:07:23
字体:
来源:转载
供稿:网友
/* * 下划线转驼峰 */PRivate function convertUnderline($str){    $str = preg_replace_callback('/([-_]+([a-z]{1}))/i',function($matches){        return strtoupper($matches[2]);    },$str);    return $str;}/* * 驼峰转下划线 */private function humpToLine($str){    $str = preg_replace_callback('/([A-Z]{1})/',function($matches){        return '_'.strtolower($matches[0]);    },$str);    return $str;}private function convertHump(array $data){    $result = [];    foreach ($data as $key => $item) {        if (is_array($item) || is_object($item)) {            $result[$this->humpToLine($key)] = $this->convertHump((array)$item);        } else {            $result[$this->humpToLine($key)] = $item;        }    }    return $result;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表