명

from_camel_case, to_camel_case

· 2011-12-19 (월) 19:37:10 · 2947
표기법이 달라 같은 엔티티 참조가 어려울때, 한쪽의 표기법을 변경하는 방법
자바나 자바스크립트 변수명 또는 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
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,188건
+
제목 글쓴이 날짜 조회
16-03-21 조회 3,107
15-12-03 조회 2,959
14-09-21 조회 4,424
14-09-21 조회 3,912
14-09-20 조회 3,915
14-09-20 조회 3,817
14-09-20 조회 4,001
14-09-19 조회 3,917
14-09-19 조회 4,078
14-09-19 조회 3,969
14-09-19 조회 4,001
14-09-19 조회 3,923
14-09-19 조회 4,209
14-09-19 조회 3,946
14-09-19 조회 3,994
14-09-19 조회 3,969
14-09-18 조회 4,042
14-09-18 조회 4,006
14-09-18 조회 4,270
14-09-18 조회 4,244
14-09-18 조회 4,246
14-09-18 조회 4,953
14-07-25 조회 4,078
13-05-18 조회 5,819
13-03-13 조회 8,387
13-02-16 조회 6,976
13-02-05 조회 7,690
13-01-28 조회 6,847
13-01-28 조회 6,790
13-01-25 조회 5,152
13-01-25 조회 1.1만
13-01-24 조회 4,458
13-01-18 조회 4,003
13-01-18 조회 6,226
13-01-18 조회 5,908
13-01-17 조회 4,444
13-01-17 조회 5,201
13-01-14 조회 5,255
13-01-03 조회 1만
12-12-29 조회 4,641
12-11-29 조회 1.2만
12-11-16 조회 6,478
12-10-22 조회 8,538
12-09-11 조회 4,267
12-09-11 조회 3,917
12-09-11 조회 3,737
12-09-11 조회 3,705
12-09-03 조회 4,678
12-08-29 조회 3,625
12-08-27 조회 4,082
12-08-22 조회 3,524
12-08-19 조회 3,890
12-08-16 조회 3,435
12-08-02 조회 3,850
12-07-17 조회 3,588
12-07-14 조회 4,357
12-07-12 조회 4,391
12-07-10 조회 5,713
12-07-05 조회 3,501
12-06-08 조회 4,954
12-05-19 조회 3,442
12-05-18 조회 5,038
12-04-09 조회 4,772
12-04-05 조회 3,651
12-02-27 조회 3,866
12-02-11 조회 3,478
12-02-09 조회 3,948
12-02-09 조회 3,518
12-01-17 조회 5,209
12-01-10 조회 5,349
12-01-10 조회 5,523
12-01-10 조회 5,409
12-01-10 조회 5,313
11-12-23 조회 3,604
11-12-19 조회 6,272
11-12-19 조회 5,024
11-12-09 조회 4,170
11-12-09 조회 3,782
11-12-09 조회 3,983
11-12-09 조회 4,493