보통 입력된 데이터 형식(전화번호, 영/숫자 아이디 등..)을 판단하기 위해 정규 표현식을 사용할 수도 있지만 PHP 함수를 사용하는 것이 언제나 더 바람직하다.
왜냐하면 PHP 함수들은 직접 작성한 코드보다 더 적은 오류를 가질 가능성이 높고 직접 작성한 필터링 로직의 오류는 거의 대부분 보안 취약점으로 이어지기 때문이다.
그래서 PHP는 ctype 함수들을 제공한다.
output:
왜냐하면 PHP 함수들은 직접 작성한 코드보다 더 적은 오류를 가질 가능성이 높고 직접 작성한 필터링 로직의 오류는 거의 대부분 보안 취약점으로 이어지기 때문이다.
그래서 PHP는 ctype 함수들을 제공한다.
- ctype_alnum
영문/숫자인지 체크하여 TRUE나 FALSE를 반환한다. (abc123) - ctype_alpha
영문자인지 체크하여 TRUE나 FALSE를 반환한다. (abc) - ctype_cntrl
특별한 제어 함수인지 체크하여 TRUE나 FALSE를 반환한다. (\n\r\t) - ctype_digit
숫자인지 체크하여 TRUE나 FALSE를 반환한다. (123) - ctype_graph
인쇄가능한 문자인지 체크하여 TRUE나 FALSE를 반환한다. (빈칸, \n 등 제외) - ctype_lower
영소문자인지 체크하여 TRUE나 FALSE를 반환한다. (abc) - ctype_print
인쇄가능한 문자인지 체크하여 TRUE나 FALSE를 반환한다. (\n 등 제외) - ctype_punct
빈칸을 제외한 특수문자인지 체크하여 TRUE나 FALSE를 반환한다. (@#$%) - ctype_space
공백문자인지 체크하여 TRUE나 FALSE를 반환한다. (\n\r\t) - ctype_upper
영대문자인지 체크하여 TRUE나 FALSE를 반환한다. (ABC) - ctype_xdigit
16진수 문자인지 체크하여 TRUE나 FALSE를 반환한다. (ab12bc99)
<?php
$strings = array( 'AbCd1zyZ9', 'foo!#$bar' );
foreach ( $strings as $testcase ) {
if ( ctype_alnum( $testcase ) ) {
echo "The string $testcase consists of all letters or digits.\n";
} else {
echo "The string $testcase does not consist of all letters or digits.\n";
}
}
?>
$strings = array( 'AbCd1zyZ9', 'foo!#$bar' );
foreach ( $strings as $testcase ) {
if ( ctype_alnum( $testcase ) ) {
echo "The string $testcase consists of all letters or digits.\n";
} else {
echo "The string $testcase does not consist of all letters or digits.\n";
}
}
?>
output:
The string AbCd1zyZ9 consists of all letters or digits.
The string foo!#$bar does not consists of all letters or digits.
The string foo!#$bar does not consists of all letters or digits.
WRITTEN BY
- 손가락귀신
정신 못차리면, 벌 받는다.
,