PHP preg_quote() 함수
예시
정규 표현식에서 특수 문자를 안전하게 사용하려면 preg_quote()를 사용하십시오.
<?php
$search = preg_quote("://", "/");
$input = 'https://www.w3schools.com/';
$pattern = "/$search/";
if(preg_match($pattern, $input)) {
echo
"The input is a URL.";
} else {
echo "The input is not a URL.";
}
?>
정의 및 사용
이 preg_quote()
함수는 리터럴 문자를 검색할 수 있도록 정규식에서 특별한 의미를 갖는 문자에 백슬래시를 추가합니다. 이 함수는 정규식에서 사용자 입력을 사용할 때 유용합니다.
통사론
preg_quote(input, delimiter)
매개변수 값
Parameter | Description |
---|---|
input | Required. The string to be escaped |
delimiter | Optional. Defaults to null. This parameter expects a single character indicating which delimiter the regular expression will use. When provided, instances of this character in the input string will also be escaped with a backslash |
기술적 세부 사항
반환 값: | 백슬래시로 이스케이프된 모든 특수 문자가 포함된 문자열을 반환합니다. |
---|---|
PHP 버전: | 4+ |
변경 로그: | PHP 7.3 - # 문자는 이제 특수 문자로 간주되어 이스케이프 처리됩니다. PHP 5.3 - - 문자는 이제 특수 문자로 간주되어 이스케이프됩니다. |
❮ PHP RegExp 참조