PHP preg_match_all() လုပ်ဆောင်ချက်
ဥပမာ
string တစ်ခုတွင် "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 ၏အရှည်ထက် ပိုရှည်သောအခါတွင် လုပ်ဆောင်ချက်သည် မှားယွင်းသွားသောအခါတွင် ထည့်သွင်းမှု PHP 5.2.2 - အမည်ပေးထားသော ပုံစံခွဲများသည် (?'name' ကို အသုံးပြုနိုင်သည်။ ယခင် (?P<name>) အပြင် ) နှင့် (? <name>) syntax |
နောက်ထပ် ဥပမာများ
ဥပမာ
ကိုက်ညီ သော array ၏ ဖွဲ့စည်းပုံကို သတ်မှတ်ရန် PREG_PATTERN_ORDER ကို အသုံးပြုပါ ။ ဤဥပမာတွင်၊ match array အတွင်းရှိ element တစ်ခုစီတွင် ပုံမှန် expression ၏ groupings များထဲမှ တစ်ခုအတွက် ကိုက်ညီမှုအားလုံးရှိသည်။
<?php
$str = "abc ABC";
$pattern = "/((a)b)(c)/i";
if(preg_match_all($pattern,
$str, $matches, PREG_PATTERN_ORDER)) {
print_r($matches);
}
?>
❮ PHP RegExp အကိုးအကား