from_camel_case, to_camel_case

· 14년 전 · 1566
표기법이 달라 같은 엔티티 참조가 어려울때, 한쪽의 표기법을 변경하는 방법
자바나 자바스크립트 변수명 또는 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,256
14년 전 조회 5,354
14년 전 조회 2,236
14년 전 조회 2,014
14년 전 조회 1,504
14년 전 조회 1,751
14년 전 조회 1,561
14년 전 조회 1,432
14년 전 조회 1,536
14년 전 조회 1,511
14년 전 조회 1,470
14년 전 조회 2,320
14년 전 조회 1,645
14년 전 조회 1,524
14년 전 조회 1,451
14년 전 조회 1,524
14년 전 조회 1,838
14년 전 조회 1,753
14년 전 조회 2,345
14년 전 조회 1,930
14년 전 조회 4,400
14년 전 조회 1,635
14년 전 조회 2,197
14년 전 조회 1,923
14년 전 조회 3,651
14년 전 조회 3,286
14년 전 조회 1,444
14년 전 조회 2,504
14년 전 조회 1,478
14년 전 조회 3,282
14년 전 조회 2,843
14년 전 조회 1,433
14년 전 조회 6,554
14년 전 조회 1,796
14년 전 조회 2,352
14년 전 조회 2,131
14년 전 조회 2,060
14년 전 조회 2,037
14년 전 조회 2,046
14년 전 조회 1,878
14년 전 조회 2,241
14년 전 조회 2,610
14년 전 조회 1,643
14년 전 조회 2,034
14년 전 조회 3,039
14년 전 조회 2,896
14년 전 조회 1,886
14년 전 조회 2,349
14년 전 조회 4,846
14년 전 조회 1,567
14년 전 조회 3,561
14년 전 조회 2,302
14년 전 조회 2,773
14년 전 조회 1,985
14년 전 조회 1,751
14년 전 조회 1,750
14년 전 조회 1,882
14년 전 조회 1,967
14년 전 조회 2,855
14년 전 조회 4,078
14년 전 조회 3,156
14년 전 조회 1,885
14년 전 조회 2,449
14년 전 조회 2,701
14년 전 조회 1,516
14년 전 조회 2,772
14년 전 조회 4,002
14년 전 조회 2,648
14년 전 조회 3,591
14년 전 조회 4,840
14년 전 조회 4,963
14년 전 조회 3,362
14년 전 조회 1,789
14년 전 조회 2,112
14년 전 조회 4,903
14년 전 조회 3,211
14년 전 조회 2,371
14년 전 조회 4,767
14년 전 조회 3,412
14년 전 조회 1,519
14년 전 조회 3,417
14년 전 조회 1,548
14년 전 조회 1,803
14년 전 조회 2,652
14년 전 조회 2,281
14년 전 조회 2,519
14년 전 조회 2,051
14년 전 조회 3,063
14년 전 조회 1,722
14년 전 조회 1,746
14년 전 조회 1,577
14년 전 조회 2,387
14년 전 조회 1,991
14년 전 조회 1,703
14년 전 조회 6,260
14년 전 조회 2,842
14년 전 조회 2,104
14년 전 조회 1,606
14년 전 조회 1만
14년 전 조회 2,498