{"id":167,"date":"2023-02-19T17:30:51","date_gmt":"2023-02-19T09:30:51","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=167"},"modified":"2023-02-19T17:31:29","modified_gmt":"2023-02-19T09:31:29","slug":"php-underline-hump-string-conversion","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/19\/php-underline-hump-string-conversion\/","title":{"rendered":"PHP\u4e0b\u5212\u7ebf\u548c\u9a7c\u5cf0\u5b57\u7b26\u4e32\u76f8\u4e92\u8f6c\u6362"},"content":{"rendered":"<p>PHP\u4e0b\u5212\u7ebf\u548c\u9a7c\u5cf0\u5b57\u7b26\u4e32\u76f8\u4e92\u8f6c\u6362<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-php\">class AppBlogString\n{\n    \/*\n     * \u4e0b\u5212\u7ebf\u8f6c\u9a7c\u5cf0\n     *\/\n    public static function convertUnderline($str)\n    {\n        $str = preg_replace_callback(&#039;\/([-_]+([a-z]{1}))\/i&#039;, function ($matches) {\n            return strtoupper($matches[2]);\n        }, $str);\n        return $str;\n    }\n\n    \/*\n     * \u9a7c\u5cf0\u8f6c\u4e0b\u5212\u7ebf\n     *\/\n    public static function humpToLine($str)\n    {\n        $str = preg_replace_callback(&#039;\/([A-Z]{1})\/&#039;, function ($matches) use ($str) {\n            if ($matches[0] != substr($str, 0, 1))\n                return &#039;_&#039; . strtolower($matches[0]);\n            else\n                return strtolower($matches[0]);\n        }, $str);\n        return $str;\n    }\n\n    public static function convertHump(array $data)\n    {\n        $result = [];\n        foreach ($data as $key =&gt; $item) {\n            if (is_array($item) || is_object($item)) {\n                $result[$this-&gt;humpToLine($key)] = $this-&gt;convertHump((array) $item);\n            } else {\n                $result[$this-&gt;humpToLine($key)] = $item;\n            }\n        }\n        return $result;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u4e0b\u5212\u7ebf\u548c\u9a7c\u5cf0\u5b57\u7b26\u4e32\u76f8\u4e92\u8f6c\u6362 class AppBlogString { \/* * \u4e0b\u5212\u7ebf\u8f6c\u9a7c\u5cf0 *\/ p [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[],"class_list":["post-167","post","type-post","status-publish","format-standard","hentry","category-php-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=167"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/167\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}