PHP preg_match() İşlevi
Örnek
Bir dizede "w3schools" için büyük/küçük harfe duyarlı olmayan bir arama yapmak için normal bir ifade kullanın:
<?php
$str = "Visit W3Schools";
$pattern = "/w3schools/i";
echo
preg_match($pattern, $str);
?>
Tanım ve Kullanım
İşlev preg_match()
, bir dizede bir eşleşme bulunup bulunmadığını döndürür.
Sözdizimi
preg_match(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:
|
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: | Bir eşleşme bulunursa 1, eşleşme bulunmazsa 0 ve bir hata meydana gelirse false döndürür |
---|---|
PHP Sürümü: | 4+ |
Değişiklik günlüğü: | PHP 7.2 - PREG_UNMATCHED_AS_NULL bayrağı eklendi 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') ve (? <name>) öğelerini kullanabilir öncekine ek olarak sözdizimi (?P<name>) |
Daha fazla örnek
Örnek
Giriş dizesinde eşleşmelerin bulunduğu konumu bulmak için PREG_OFFSET_CAPTURE kullanın:
<?php
$str = "Welcome to W3Schools";
$pattern = "/w3schools/i";
preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
❮ PHP RegExp Referansı