from_camel_case, to_camel_case

· 14년 전 · 618
표기법이 달라 같은 엔티티 참조가 어려울때, 한쪽의 표기법을 변경하는 방법
자바나 자바스크립트 변수명 또는 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년 전 조회 1,332
14년 전 조회 4,436
14년 전 조회 1,309
14년 전 조회 1,074
14년 전 조회 571
14년 전 조회 802
14년 전 조회 625
14년 전 조회 505
14년 전 조회 588
14년 전 조회 570
14년 전 조회 509
14년 전 조회 1,383
14년 전 조회 705
14년 전 조회 560
14년 전 조회 535
14년 전 조회 593
14년 전 조회 887
14년 전 조회 792
14년 전 조회 1,376
14년 전 조회 999
14년 전 조회 3,456
14년 전 조회 673
14년 전 조회 1,230
14년 전 조회 964
14년 전 조회 2,704
14년 전 조회 2,312
14년 전 조회 519
14년 전 조회 1,579
14년 전 조회 527
14년 전 조회 2,325
14년 전 조회 1,866
14년 전 조회 483
14년 전 조회 5,567
14년 전 조회 837
14년 전 조회 1,345
14년 전 조회 1,165
14년 전 조회 1,207
14년 전 조회 1,085
14년 전 조회 1,086
14년 전 조회 916
14년 전 조회 1,271
14년 전 조회 1,660
14년 전 조회 679
14년 전 조회 1,056
14년 전 조회 2,093
14년 전 조회 1,945
14년 전 조회 899
14년 전 조회 1,394
14년 전 조회 3,881
14년 전 조회 619
14년 전 조회 2,604
14년 전 조회 1,378
14년 전 조회 1,813
14년 전 조회 1,028
14년 전 조회 800
14년 전 조회 796
14년 전 조회 942
14년 전 조회 1,015
14년 전 조회 1,898
14년 전 조회 3,150
14년 전 조회 2,197
14년 전 조회 905
14년 전 조회 1,518
14년 전 조회 1,759
14년 전 조회 561
14년 전 조회 1,804
14년 전 조회 3,040
14년 전 조회 1,721
14년 전 조회 2,658
14년 전 조회 3,967
14년 전 조회 3,999
14년 전 조회 2,409
14년 전 조회 847
14년 전 조회 1,138
14년 전 조회 3,960
14년 전 조회 2,279
14년 전 조회 1,430
14년 전 조회 3,798
14년 전 조회 2,475
14년 전 조회 558
14년 전 조회 2,464
14년 전 조회 610
14년 전 조회 841
14년 전 조회 1,679
14년 전 조회 1,302
14년 전 조회 1,558
14년 전 조회 1,118
14년 전 조회 2,116
14년 전 조회 791
14년 전 조회 801
14년 전 조회 630
14년 전 조회 1,433
14년 전 조회 1,049
14년 전 조회 767
14년 전 조회 5,296
14년 전 조회 1,911
14년 전 조회 1,146
14년 전 조회 649
14년 전 조회 9,440
14년 전 조회 1,558
🐛 버그신고