دالة 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 |
تفاصيل تقنية
قيمة الإرجاع: | إرجاع عدد المطابقات التي تم العثور عليها أو خطأ في حالة حدوث خطأ |
---|---|
إصدار PHP: | 4+ |
التغيير: | PHP 7.2 - تمت إضافة علامة PREG_UNMATCHED_AS_NULL PHP 5.4 - أصبحت معلمة التطابقات اختيارية PHP 5.3.6 - ترجع الدالة خطأ عندما تكون الإزاحة أطول من طول إدخال 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