명

from_camel_case, to_camel_case

· 2011-12-19 (월) 19:37:10 · 2866
표기법이 달라 같은 엔티티 참조가 어려울때, 한쪽의 표기법을 변경하는 방법
자바나 자바스크립트 변수명 또는 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-02 조회 3,214
16-01-19 조회 2,495
15-12-23 조회 2,806
15-12-08 조회 3,613
14-10-30 조회 5,660
14-06-06 조회 5,495
12-08-30 조회 3,852
12-08-20 조회 3,776
12-07-15 조회 5,103
12-07-13 조회 3,581
12-07-06 조회 3,785
12-06-27 조회 3,322
12-06-23 조회 3,516
12-05-03 조회 5,139
12-04-20 조회 4,089
12-03-29 조회 4,580
12-03-22 조회 4,662
12-03-22 조회 3,464
12-03-21 조회 3,240
12-03-11 조회 3,679
12-03-06 조회 4,218
12-02-22 조회 4,493
12-02-17 조회 3,292
12-02-17 조회 6,138
12-02-14 조회 6,063
12-02-06 조회 4,216
12-01-18 조회 3,181
12-01-06 조회 3,401
11-12-30 조회 3,871
11-12-30 조회 7,016
11-12-21 조회 3,193
11-12-21 조회 4,201
11-12-16 조회 3,235
11-12-15 조회 3,280
11-12-15 조회 3,383
11-12-15 조회 4,174
11-12-09 조회 3,198
11-12-09 조회 3,270
11-12-07 조회 2,839
11-12-07 조회 3,925
11-12-05 조회 4,378
11-12-02 조회 3,817
11-12-01 조회 2,678
11-12-01 조회 2,731
11-12-01 조회 2,716