from_camel_case, to_camel_case

· 14년 전 · 1397
표기법이 달라 같은 엔티티 참조가 어려울때, 한쪽의 표기법을 변경하는 방법
자바나 자바스크립트 변수명 또는 XML의 node, attribute name이 camel 을 쓰고,
DB Column이 underscore(_) 를 쓰죠.
( db 컬럼과 xml 의 node명을 상호 참조할경우 아래와 같이 변환해서 쓰면 좋습니다.
xml을 파싱해서 db에 넣는다던지, db를 조회해서 xml을 만든다던지..
)
 
 
<?php
  /**
   * Translates a camel case string into a string with underscores (e.g. firstName -> first_name)
   * @param    string   $str    String in camel case format
   * @return    string            $str Translated into underscore format
   */
  function from_camel_case($str) {
    $str[0] = strtolower($str[0]);
    $func = create_function('$c', 'return "_" . strtolower($c[1]);');
    return preg_replace_callback('/([A-Z])/', $func, $str);
  }
 
  /**
   * Translates a string with underscores into camel case (e.g. first_name -> firstName)
   * @param    string   $str                     String in underscore format
   * @param    bool     $capitalise_first_char   If true, capitalise the first char in $str
   * @return   string                              $str translated into camel caps
   */
  function to_camel_case($str, $capitalise_first_char = false) {
    if($capitalise_first_char) {
      $str[0] = strtoupper($str[0]);
    }
    $func = create_function('$c', 'return strtoupper($c[1]);');
    return preg_replace_callback('/_([a-z])/', $func, $str);
  }

$test1 =   "MODIFY_DATE";
$test2 =   "modifyDate";
$result1 = to_camel_case(strtolower($test1));
$result2 = strtoupper(from_camel_case($test2));
echo "$test1 => $result1 <br>";
echo "$test2 => $result2 <br>";
?>
output:
MODIFY_DATE => modifyDate
modifyDate => MODIFY_DATE
|
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
14년 전 조회 2,081
14년 전 조회 5,162
14년 전 조회 2,063
14년 전 조회 1,840
14년 전 조회 1,328
14년 전 조회 1,560
14년 전 조회 1,356
14년 전 조회 1,243
14년 전 조회 1,325
14년 전 조회 1,333
14년 전 조회 1,290
14년 전 조회 2,134
14년 전 조회 1,465
14년 전 조회 1,315
14년 전 조회 1,252
14년 전 조회 1,300
14년 전 조회 1,634
14년 전 조회 1,542
14년 전 조회 2,149
14년 전 조회 1,751
14년 전 조회 4,209
14년 전 조회 1,427
14년 전 조회 2,027
14년 전 조회 1,727
14년 전 조회 3,486
14년 전 조회 3,094
14년 전 조회 1,283
14년 전 조회 2,289
14년 전 조회 1,307
14년 전 조회 3,077
14년 전 조회 2,643
14년 전 조회 1,227
14년 전 조회 6,364
14년 전 조회 1,624
14년 전 조회 2,145
14년 전 조회 1,945
14년 전 조회 1,863
14년 전 조회 1,850
14년 전 조회 1,849
14년 전 조회 1,668
14년 전 조회 2,037
14년 전 조회 2,436
14년 전 조회 1,467
14년 전 조회 1,862
14년 전 조회 2,840
14년 전 조회 2,689
14년 전 조회 1,677
14년 전 조회 2,150
14년 전 조회 4,645
14년 전 조회 1,398
14년 전 조회 3,359
14년 전 조회 2,123
14년 전 조회 2,573
14년 전 조회 1,787
14년 전 조회 1,547
14년 전 조회 1,554
14년 전 조회 1,680
14년 전 조회 1,764
14년 전 조회 2,646
14년 전 조회 3,896
14년 전 조회 2,967
14년 전 조회 1,669
14년 전 조회 2,258
14년 전 조회 2,539
14년 전 조회 1,322
14년 전 조회 2,587
14년 전 조회 3,793
14년 전 조회 2,467
14년 전 조회 3,407
14년 전 조회 4,654
14년 전 조회 4,750
14년 전 조회 3,152
14년 전 조회 1,590
14년 전 조회 1,903
14년 전 조회 4,711
14년 전 조회 3,016
14년 전 조회 2,189
14년 전 조회 4,559
14년 전 조회 3,206
14년 전 조회 1,298
14년 전 조회 3,201
14년 전 조회 1,333
14년 전 조회 1,568
14년 전 조회 2,452
14년 전 조회 2,070
14년 전 조회 2,320
14년 전 조회 1,852
14년 전 조회 2,874
14년 전 조회 1,529
14년 전 조회 1,540
14년 전 조회 1,375
14년 전 조회 2,194
14년 전 조회 1,797
14년 전 조회 1,508
14년 전 조회 6,077
14년 전 조회 2,645
14년 전 조회 1,912
14년 전 조회 1,396
14년 전 조회 1만
14년 전 조회 2,312