from_camel_case, to_camel_case

· 14년 전 · 1573
표기법이 달라 같은 엔티티 참조가 어려울때, 한쪽의 표기법을 변경하는 방법
자바나 자바스크립트 변수명 또는 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,272
14년 전 조회 5,366
14년 전 조회 2,246
14년 전 조회 2,030
14년 전 조회 1,510
14년 전 조회 1,761
14년 전 조회 1,580
14년 전 조회 1,442
14년 전 조회 1,550
14년 전 조회 1,519
14년 전 조회 1,485
14년 전 조회 2,328
14년 전 조회 1,658
14년 전 조회 1,537
14년 전 조회 1,462
14년 전 조회 1,537
14년 전 조회 1,847
14년 전 조회 1,760
14년 전 조회 2,354
14년 전 조회 1,939
14년 전 조회 4,412
14년 전 조회 1,648
14년 전 조회 2,209
14년 전 조회 1,932
14년 전 조회 3,664
14년 전 조회 3,296
14년 전 조회 1,453
14년 전 조회 2,519
14년 전 조회 1,486
14년 전 조회 3,304
14년 전 조회 2,851
14년 전 조회 1,442
14년 전 조회 6,566
14년 전 조회 1,807
14년 전 조회 2,363
14년 전 조회 2,142
14년 전 조회 2,063
14년 전 조회 2,054
14년 전 조회 2,059
14년 전 조회 1,896
14년 전 조회 2,252
14년 전 조회 2,620
14년 전 조회 1,654
14년 전 조회 2,046
14년 전 조회 3,055
14년 전 조회 2,908
14년 전 조회 1,897
14년 전 조회 2,366
14년 전 조회 4,857
14년 전 조회 1,574
14년 전 조회 3,571
14년 전 조회 2,311
14년 전 조회 2,784
14년 전 조회 1,996
14년 전 조회 1,763
14년 전 조회 1,756
14년 전 조회 1,894
14년 전 조회 1,979
14년 전 조회 2,865
14년 전 조회 4,091
14년 전 조회 3,167
14년 전 조회 1,892
14년 전 조회 2,466
14년 전 조회 2,710
14년 전 조회 1,523
14년 전 조회 2,782
14년 전 조회 4,011
14년 전 조회 2,658
14년 전 조회 3,605
14년 전 조회 4,842
14년 전 조회 4,974
14년 전 조회 3,372
14년 전 조회 1,793
14년 전 조회 2,120
14년 전 조회 4,913
14년 전 조회 3,219
14년 전 조회 2,382
14년 전 조회 4,782
14년 전 조회 3,426
14년 전 조회 1,533
14년 전 조회 3,423
14년 전 조회 1,559
14년 전 조회 1,810
14년 전 조회 2,665
14년 전 조회 2,288
14년 전 조회 2,530
14년 전 조회 2,064
14년 전 조회 3,077
14년 전 조회 1,734
14년 전 조회 1,760
14년 전 조회 1,593
14년 전 조회 2,397
14년 전 조회 2,007
14년 전 조회 1,712
14년 전 조회 6,270
14년 전 조회 2,855
14년 전 조회 2,113
14년 전 조회 1,612
14년 전 조회 1만
14년 전 조회 2,503