'pcre'에 해당되는 글 3건

  1. 2012.07.10 정규표현식 한글
  2. 2012.07.10 PCRE 정규 표현식
  3. 2012.05.29 PCRE 정규표현식 기본기
php2012. 7. 10. 13:03




· 이제 아는 정규식을 총동원해서 규칙을 생성해주시면 됩니다! 말은 무지 간단하네요^^;


//C:\CodeIgniter_2.1.0\system\libraries\Form_validation.php

    /**
     * Convert PHP tags to entities
     *
     * @access    public
     * @param    string
     * @return    string
     */

    public function encode_php_tags($str)
    {
        return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
    }
    
    public function alpha_number_kr($str) { //특수문자, 한글, 영문, 숫자
        return ( ! preg_match("/^([\+\.\:\-_ 가-힣a-zA-Z0-9])+$/i", $str)) ? FALSE : TRUE;
    }
    
    public function alpha_kr($str) { //한글, 영문
        return ( ! preg_match("/^([가-힣a-zA-Z])+$/i", $str)) ? FALSE : TRUE;
    }
    
    public function id($str) { //맨 앞글자 반드시 영문, 그 뒤 영문, 숫자, 특수문자
        return ( ! preg_match("/^([a-zA-Z]+[a-zA-Z0-9\-\_\.])+$/i", $str)) ? FALSE : TRUE;
    }
    
    public function zero($str) { //숫자 0
        return ( $str == '0') ? FALSE : TRUE;
    }
    
    public function blank($str) { //빈칸
        return ( isset($str)) ? FALSE : TRUE;
    }
    
    public function noCheck($str) { //not 'no'
        return ( $str != 'no') ? FALSE : TRUE;
    }

Posted by 다오나무
php2012. 7. 10. 12:55



Posted by 다오나무
영삼이의 IT정보2012. 5. 29. 13:05

PCRE 정규표현식 기본기


http://kr2.php.net/manual/en/ref.pcre.php
http://kr2.php.net/manual/en/reference.pcre.pattern.syntax.php
http://kr2.php.net/manual/en/reference.pcre.pattern.modifiers.php

위 링크의 내용을 바탕으로 기본을 설명합니다.

1. \ : 정규표현식에 사용되는 기호를 문자로써 사용하기 위해 탈출(escape) 시키는 문자.
<?php
preg_match('/\//', '---/---', $matches);
echo $matches[0];
?>
/

2. . : 아무거나 한 글자를 의미 (UTF-8 에서는 글자단위 그 외에는 byte 단위) (개행문자 제외)
3. ^ : 시작을 의미
<?php
echo preg_replace('/^./', '', 'abcd');
?>
bcd

4. $ : 끝을 의미
<?php
echo preg_replace('/.$/', '', 'abcd');
?>
abc

5. * : 앞의 기호의 반복을 의미 없어도 참
6. + : 앞의 기호의 반복을 의미 없으면 거짓
7. [] : [] 안의 내용 중 한글자를 의미 (- 로 구간설정 가능)
8. \d : [0-9] 를 의미
<?php
if(!preg_match('/^[1-9]\d*$/', $_GET['no'])) {
    echo '1 부터 시작하는 수를 적으셔';
    exit;
}
if(!preg_match('/^[a-z]+$/', $_GET['text'])) {
    echo '영문 소문자만 적으셔';
}
?>

9. \s : 공백문자를 의미 (탭 포함)
10. \t : 탭을 의미
11. \r : 개행문자 0x0d 를 의미
12. \n : 개행문자 0x0a 를 의미
<?php
$text = <<<TEXT
가나다 김삼순
킹왕짱     어쭈구리

abcd
TEXT;
print_r(preg_split('/[\s\r\n]+/', $text));
$text = <<<TEXT

?>
Array
(
     [0] => 가나다
     [1] => 김삼순
     [2] => 킹왕짱
     [3] => 어쭈구리
     [4] => abcd
)
<?php
$text = <<<TEXT
이름\t진\t선\t미
김태희\t80\t90\t100
송혜교\t100\t100\t98
TEXT;
$rows = preg_split('/[\r\n]+/', $text);
foreach($rows as $row) {
    $cols[] = preg_split('/\t/', trim($row));
}
print_r($cols);
?>
Array
(
     [0] => Array
         (
             [0] => 이름
             [1] => 진
             [2] => 선
             [3] => 미
         )

     [1] => Array
         (
             [0] => 김태희
             [1] => 80
             [2] => 90
             [3] => 100
         )

     [2] => Array
         (
             [0] => 송혜교
             [1] => 100
             [2] => 100
             [3] => 98
         )

)

13. ? : 존재유무 ()괄호와 함께 쓸 수 있다.
<?php
$text = <<<TEXT
<input type="text" name=asdf value="asdf" />
TEXT;
$cnt = preg_match_all('@([a-z]+)\s*=\s*"?([a-z]+)@', $text, $matches);
unset($matches[0]);
print_r($matches);
?>
Array
(
     [1] => Array
         (
             [0] => type
             [1] => name
             [2] => value
         )

     [2] => Array
         (
             [0] => text
             [1] => asdf
             [2] => asdf
         )

)
<?php
$text = <<<TEXT
<p>asdf <strong>zxcv</strong></p>
<p><strong>zxcv</strong> zxcv zxcv</p>
TEXT;
echo preg_replace('@(<strong>)?zxcv(</strong>)?@', 'qwer', $text);
?>
<p>asdf qwer</p>
<p>qwer qwer qwer</p>

14. .*?, .+? : 매치범위를 최소화 한다.
<?php
$text = <<<TEXT
<a href="a">a</a><a href="b">b</a><a href="c">c</a>
TEXT;
preg_match('@<a .*>@', $text, $matches);
echo $matches[0]."\n";
preg_match('@<a .*?>@', $text, $matches);
echo $matches[0]."\n";
?>
<a href="a">a</a><a href="b">b</a><a href="c">c</a>
<a href="a">

Posted by 다오나무