from_camel_case, to_camel_case

· 14년 전 · 1649
표기법이 달라 같은 엔티티 참조가 어려울때, 한쪽의 표기법을 변경하는 방법
자바나 자바스크립트 변수명 또는 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,329
14년 전 조회 5,435
14년 전 조회 2,310
14년 전 조회 2,081
14년 전 조회 1,574
14년 전 조회 1,806
14년 전 조회 1,640
14년 전 조회 1,494
14년 전 조회 1,603
14년 전 조회 1,578
14년 전 조회 1,534
14년 전 조회 2,393
14년 전 조회 1,718
14년 전 조회 1,590
14년 전 조회 1,513
14년 전 조회 1,597
14년 전 조회 1,896
14년 전 조회 1,804
14년 전 조회 2,408
14년 전 조회 2,012
14년 전 조회 4,474
14년 전 조회 1,709
14년 전 조회 2,275
14년 전 조회 1,988
14년 전 조회 3,713
14년 전 조회 3,354
14년 전 조회 1,513
14년 전 조회 2,575
14년 전 조회 1,556
14년 전 조회 3,367
14년 전 조회 2,912
14년 전 조회 1,512
14년 전 조회 6,625
14년 전 조회 1,877
14년 전 조회 2,421
14년 전 조회 2,197
14년 전 조회 2,094
14년 전 조회 2,111
14년 전 조회 2,108
14년 전 조회 1,966
14년 전 조회 2,307
14년 전 조회 2,685
14년 전 조회 1,710
14년 전 조회 2,102
14년 전 조회 3,116
14년 전 조회 2,980
14년 전 조회 1,960
14년 전 조회 2,426
14년 전 조회 4,923
14년 전 조회 1,650
14년 전 조회 3,634
14년 전 조회 2,369
14년 전 조회 2,848
14년 전 조회 2,065
14년 전 조회 1,830
14년 전 조회 1,834
14년 전 조회 1,969
14년 전 조회 2,047
14년 전 조회 2,941
14년 전 조회 4,167
14년 전 조회 3,233
14년 전 조회 1,954
14년 전 조회 2,543
14년 전 조회 2,785
14년 전 조회 1,597
14년 전 조회 2,847
14년 전 조회 4,075
14년 전 조회 2,747
14년 전 조회 3,675
14년 전 조회 4,873
14년 전 조회 5,048
14년 전 조회 3,432
14년 전 조회 1,849
14년 전 조회 2,189
14년 전 조회 4,977
14년 전 조회 3,294
14년 전 조회 2,445
14년 전 조회 4,848
14년 전 조회 3,480
14년 전 조회 1,586
14년 전 조회 3,484
14년 전 조회 1,612
14년 전 조회 1,868
14년 전 조회 2,725
14년 전 조회 2,349
14년 전 조회 2,583
14년 전 조회 2,129
14년 전 조회 3,141
14년 전 조회 1,799
14년 전 조회 1,816
14년 전 조회 1,642
14년 전 조회 2,442
14년 전 조회 2,070
14년 전 조회 1,770
14년 전 조회 6,314
14년 전 조회 2,915
14년 전 조회 2,173
14년 전 조회 1,675
14년 전 조회 1만
14년 전 조회 2,582