PHP preg_match_all() 함수
예시
문자열에서 "ain"의 모든 항목 찾기:
<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern =
"/ain/i";
if(preg_match_all($pattern, $str, $matches)) {
print_r($matches);
}
?>
정의 및 사용
이 preg_match_all()
함수는 문자열에서 발견된 패턴의 일치 수를 반환하고 발견된 일치로 변수를 채웁니다.
통사론
preg_match_all(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. One of the following structures may be selected:
|
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 |
기술적 세부 사항
반환 값: | 발견된 일치 항목의 수를 반환하거나 오류가 발생한 경우 false를 반환합니다. |
---|---|
PHP 버전: | 4+ |
변경 로그: | PHP 7.2 - PREG_UNMATCHED_AS_NULL 플래그 추가 PHP 5.4 - 일치 매개변수가 선택 사항이 되었습니다. PHP 5.3.6 - 오프셋이 입력의 길이보다 길면 함수가 false를 반환합니다. PHP 5.2.2 - 명명된 하위 패턴은 (?'name' ) 및 (? <name>) 구문과 이전 (?P<name>) 구문 |
더 많은 예
예시
PREG_PATTERN_ORDER를 사용하여 일치 배열의 구조를 설정합니다. 이 예에서 일치 배열 의 각 요소 에는 정규식 그룹 중 하나에 대한 모든 일치 항목이 있습니다.
<?php
$str = "abc ABC";
$pattern = "/((a)b)(c)/i";
if(preg_match_all($pattern,
$str, $matches, PREG_PATTERN_ORDER)) {
print_r($matches);
}
?>
❮ PHP RegExp 참조