PHP preg_match() 함수
예시
정규식을 사용하여 문자열에서 "w3schools"에 대해 대소문자를 구분하지 않고 검색합니다.
<?php
$str = "Visit W3Schools";
$pattern = "/w3schools/i";
echo
preg_match($pattern, $str);
?>
정의 및 사용
이 preg_match()
함수는 문자열에서 일치 항목이 발견되었는지 여부를 반환합니다.
통사론
preg_match(pattern, input, matches, flags, offset)
매개변수 값
Parameter | Description |
---|---|
pattern | Required. Contains a regular expression indicating what to search for |
input | Required. The string in which the search will be performed |
matches | Optional. The variable used in this parameter will be populated with an array containing all of the matches that were found |
flags | Optional. A set of options that change how the matches array is
structured:
|
offset | Optional. Defaults to 0. Indicates how far into the string to begin searching. The preg_match() function will not find matches that occur before the position given in this parameter |
기술적 세부 사항
반환 값: | 일치하는 항목이 있으면 1을 반환하고 일치하는 항목이 없으면 0을 반환하고 오류가 발생하면 false를 반환합니다. |
---|---|
PHP 버전: | 4+ |
변경 로그: | PHP 7.2 - PREG_UNMATCHED_AS_NULL 플래그 추가 PHP 5.3.6 - 오프셋이 입력의 길이보다 길면 함수가 false를 반환합니다. PHP 5.2.2 - 명명된 하위 패턴은 (?'이름') 및 (? <이름>)을 사용할 수 있습니다. 이전 구문(?P<name>) 외에 구문 |
더 많은 예
예시
PREG_OFFSET_CAPTURE를 사용하여 일치 항목이 발견된 입력 문자열의 위치를 찾습니다.
<?php
$str = "Welcome to W3Schools";
$pattern = "/w3schools/i";
preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
❮ PHP RegExp 참조