PHP preg_match_all() İşlevi
Örnek
Bir dizedeki tüm "ain" oluşumlarını bulun:
<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern =
"/ain/i";
if(preg_match_all($pattern, $str, $matches)) {
print_r($matches);
}
?>
Tanım ve Kullanım
İşlev preg_match_all()
, bir dizgede bulunan bir kalıbın eşleşme sayısını döndürür ve bulunan eşleşmelerle bir değişkeni doldurur.
Sözdizimi
preg_match_all(pattern, input, matches, flags, offset)
Parametre Değerleri
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 |
Teknik detaylar
Geri dönüş değeri: | Bulunan eşleşme sayısını döndürür veya bir hata oluşursa yanlıştır |
---|---|
PHP Sürümü: | 4+ |
Değişiklik günlüğü: | PHP 7.2 - PREG_UNMATCHED_AS_NULL bayrağı eklendi PHP 5.4 - Eşleşme parametresi isteğe bağlı hale geldi PHP 5.3.6 - Uzaklık giriş uzunluğundan uzun olduğunda işlev false döndürür PHP 5.2.2 - Adlandırılmış alt modeller (?'name') kullanabilir ) ve (? <ad>) sözdizimine ek olarak önceki (?P<ad>) |
Daha fazla örnek
Örnek
Eşleşme dizisinin yapısını ayarlamak için PREG_PATTERN_ORDER kullanın . Bu örnekte, eşleşmeler dizisindeki her öğe, normal ifadenin gruplamalarından biri için tüm eşleşmelere sahiptir.
<?php
$str = "abc ABC";
$pattern = "/((a)b)(c)/i";
if(preg_match_all($pattern,
$str, $matches, PREG_PATTERN_ORDER)) {
print_r($matches);
}
?>
❮ PHP RegExp Referansı