PHP preg_match() လုပ်ဆောင်ချက်
ဥပမာ
string တစ်ခုရှိ "w3schools" အတွက် case-sensitive ရှာဖွေမှုပြုလုပ်ရန် ပုံမှန်အသုံးအနှုန်းကိုသုံးပါ-
<?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 နှင့် အမှားအယွင်းတစ်ခုဖြစ်ပွားပါက ကိုက်ညီမှုမရှိပါက 0 ပြန်ပေးသည် |
---|---|
PHP ဗားရှင်း- | 4+ |
ပြောင်းလဲမှုမှတ်တမ်း- | PHP 7.2 - PREG_UNMATCHED_AS_NULL အလံကို ထည့်ခဲ့သည် PHP 5.3.6 - အော့ဖ်ဆက်သည် ထည့်သွင်းသည့် PHP 5.2.2 ၏အရှည်ထက် ပိုရှည်သောအခါ - အမည်ပေးထားသော ပုံစံခွဲများသည် (?'name') နှင့် (? <name>) ကိုသုံးနိုင်သည် ယခင် (?P<name>) အပြင် syntax |
နောက်ထပ် ဥပမာများ
ဥပမာ
ကိုက်ညီမှုများကို တွေ့ရှိခဲ့သည့် ထည့်သွင်းစာကြောင်း၏ အနေအထားကို ရှာဖွေရန် PREG_OFFSET_CAPTURE ကို အသုံးပြုပါ-
<?php
$str = "Welcome to W3Schools";
$pattern = "/w3schools/i";
preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
❮ PHP RegExp အကိုးအကား