美文网首页PHP
PHP工具-驼峰下划线互转

PHP工具-驼峰下划线互转

作者: 保儿洁 | 来源:发表于2018-07-26 00:56 被阅读6次
<?php

    function lineToHump($str)
    {
        return preg_replace_callback('/(_[a-z])/', function ($match) {
            return ucfirst(trim($match[0], '_'));
        }, $str);
    }

    function humpToLine($str)
    {
        return preg_replace_callback('/([A-Z])/', function ($match) {
            return '_' . lcfirst($match[0]);
        }, $str);
    }
    echo lineToHump("new_id");
    echo "\n";
    echo humpToLine("newId");

newId
new_id

相关文章

网友评论

    本文标题:PHP工具-驼峰下划线互转

    本文链接:https://www.haomeiwen.com/subject/wtkvmftx.html