دالة 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 إذا لم يتم العثور على مطابقات وإرجاع 1 إذا حدث خطأ |
---|---|
إصدار PHP: | 4+ |
التغيير: | PHP 7.2 - تمت إضافة علامة PREG_UNMATCHED_AS_NULL PHP 5.3.6 - ترجع الدالة خطأ عندما تكون الإزاحة أطول من طول إدخال PHP 5.2.2 - يمكن للأنماط الفرعية المسماة استخدام (؟ 'name') و (؟ <name>) بناء الجملة بالإضافة إلى السابق (؟ 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