LoginSignup
3
1

More than 5 years have passed since last update.

【PHP】キャメルケース⇔スネークケースの変換方法

Last updated at Posted at 2018-05-15
<?php

function convCamelize($str) {
    return str_replace(' ', '', ucwords(str_replace('_', ' ', $str)));
}

function convSnake($str) {
    return strtolower(preg_replace('/[A-Z]/', '_$0', lcfirst($str)));
}

// スネーク→キャメル
$str = 'hoge_hoge';
echo "before : ".$str." after : ".convCamelize($str).PHP_EOL;

// キャメル→スネーク
$str = 'hogeHoge';
echo "before : ".$str." after : ".convSnake($str).PHP_EOL;
$ php test.php
before : hoge_hoge after : HogeHoge
before : hogeHoge after : hoge_hoge
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1