from_camel_case, to_camel_case

· 14년 전 · 1201
표기법이 달라 같은 엔티티 참조가 어려울때, 한쪽의 표기법을 변경하는 방법
자바나 자바스크립트 변수명 또는 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,908
14년 전 조회 4,980
14년 전 조회 1,894
14년 전 조회 1,654
14년 전 조회 1,160
14년 전 조회 1,388
14년 전 조회 1,181
14년 전 조회 1,086
14년 전 조회 1,143
14년 전 조회 1,156
14년 전 조회 1,111
14년 전 조회 1,965
14년 전 조회 1,275
14년 전 조회 1,134
14년 전 조회 1,074
14년 전 조회 1,148
14년 전 조회 1,460
14년 전 조회 1,366
14년 전 조회 2,043
14년 전 조회 1,577
14년 전 조회 4,035
14년 전 조회 1,247
14년 전 조회 1,860
14년 전 조회 1,558
14년 전 조회 3,370
14년 전 조회 2,982
14년 전 조회 1,115
14년 전 조회 2,111
14년 전 조회 1,138
14년 전 조회 2,881
14년 전 조회 2,530
14년 전 조회 1,060
14년 전 조회 6,252
14년 전 조회 1,437
14년 전 조회 2,035
14년 전 조회 1,759
14년 전 조회 1,731
14년 전 조회 1,670
14년 전 조회 1,668
14년 전 조회 1,480
14년 전 조회 1,850
14년 전 조회 2,248
14년 전 조회 1,289
14년 전 조회 1,674
14년 전 조회 2,657
14년 전 조회 2,522
14년 전 조회 1,483
14년 전 조회 1,963
14년 전 조회 4,476
14년 전 조회 1,202
14년 전 조회 3,183
14년 전 조회 1,997
14년 전 조회 2,400
14년 전 조회 1,594
14년 전 조회 1,373
14년 전 조회 1,385
14년 전 조회 1,503
14년 전 조회 1,577
14년 전 조회 2,486
14년 전 조회 3,717
14년 전 조회 2,788
14년 전 조회 1,494
14년 전 조회 2,096
14년 전 조회 2,370
14년 전 조회 1,138
14년 전 조회 2,417
14년 전 조회 3,616
14년 전 조회 2,297
14년 전 조회 3,224
14년 전 조회 4,530
14년 전 조회 4,556
14년 전 조회 2,968
14년 전 조회 1,479
14년 전 조회 1,696
14년 전 조회 4,530
14년 전 조회 2,854
14년 전 조회 2,006
14년 전 조회 4,387
14년 전 조회 3,035
14년 전 조회 1,114
14년 전 조회 3,015
14년 전 조회 1,169
14년 전 조회 1,391
14년 전 조회 2,272
14년 전 조회 1,881
14년 전 조회 2,145
14년 전 조회 1,670
14년 전 조회 2,707
14년 전 조회 1,325
14년 전 조회 1,339
14년 전 조회 1,194
14년 전 조회 2,003
14년 전 조회 1,632
14년 전 조회 1,334
14년 전 조회 5,949
14년 전 조회 2,476
14년 전 조회 1,781
14년 전 조회 1,212
14년 전 조회 9,969
14년 전 조회 2,144