PHP ပုံမှန်အသုံးအနှုန်းများ
Regular Expression ဆိုတာ ဘာလဲ။
ပုံမှန်အသုံးအနှုန်းသည် ရှာဖွေမှုပုံစံတစ်ခုဖန်တီးသည့် ဇာတ်ကောင်များ၏ အတွဲလိုက်တစ်ခုဖြစ်သည်။ စာသားတစ်ခုတွင် ဒေတာကို သင်ရှာဖွေသောအခါ၊ သင်ရှာဖွေနေသည့်အရာကို ဖော်ပြရန် ဤရှာဖွေမှုပုံစံကို အသုံးပြုနိုင်သည်။
ပုံမှန်ဖော်ပြချက်တစ်ခုသည် စာလုံးတစ်လုံးတည်း သို့မဟုတ် ပိုမိုရှုပ်ထွေးသောပုံစံတစ်ခု ဖြစ်နိုင်သည်။
စာသားရှာဖွေမှု အမျိုးအစားအားလုံးကို လုပ်ဆောင်ရန်နှင့် စာသားအစားထိုးလုပ်ဆောင်မှုများကို လုပ်ဆောင်ရန် ပုံမှန်အသုံးအနှုန်းများကို အသုံးပြုနိုင်သည်။
အထားအသို
PHP တွင်၊ ပုံမှန်အသုံးအနှုန်းများသည် ကန့်သတ်ချက်များ၊ ပုံစံတစ်ခုနှင့် ရွေးချယ်နိုင်သော ပြုပြင်မွမ်းမံမှုများဖြင့် ဖွဲ့စည်းထားသည့် စာကြောင်းများဖြစ်သည်။
$exp = "/w3schools/i";
အထက်ဖော်ပြပါ ဥပမာတွင်၊ /
က န့်သတ် ချက်ဖြစ်သည်၊ w3schools သည် ရှာဖွေနေ သည့် ပုံစံ ဖြစ်ပြီး၊ ရှာဖွေမှုကိစ္စရပ်ကို အာရုံမစိုက်နိုင်ဖြစ်စေ သည့် မွမ်းမံ မှု တစ်ခုဖြစ်သည်။i
အနားသတ်သည် အက္ခရာ၊ နံပါတ်၊ backslash သို့မဟုတ် space မဟုတ်သည့် မည်သည့်အက္ခရာမဆို ဖြစ်နိုင်သည်။ အသုံးအများဆုံး မျဉ်းစောင်းများသည် ရှေ့သို့မျဉ်းစောင်း (/) ဖြစ်သော်လည်း၊ သင်၏ပုံစံတွင် ရှေ့သို့မျဉ်းစောင်းများပါရှိသောအခါ၊ # သို့မဟုတ် ~ ကဲ့သို့သော အခြားကန့်သတ်ချက်များကို ရွေးချယ်ရန် အဆင်ပြေသည်။
ပုံမှန်ဖော်ပြမှုလုပ်ဆောင်ချက်များ
PHP သည် သင့်အား ပုံမှန်အသုံးအနှုန်းများကို သုံးနိုင်စေမည့် လုပ်ဆောင်ချက်များ အမျိုးမျိုးကို ပံ့ပိုးပေးပါသည်။ preg_match()
နှင့် preg_match_all()
လုပ်ဆောင်ချက်များ preg_replace()
သည် အသုံးအများဆုံးအရာများထဲမှ အချို့ဖြစ်သည် -
Function | Description |
---|---|
preg_match() | Returns 1 if the pattern was found in the string and 0 if not |
preg_match_all() | Returns the number of times the pattern was found in the string, which may also be 0 |
preg_replace() | Returns a new string where matched patterns have been replaced with another string |
preg_match() ကိုအသုံးပြုခြင်း
preg_match()
စာကြောင်းတစ်ခုတွင် ပုံစံတစ်ခု၏ ကိုက်ညီမှုရှိမရှိ လုပ်ဆောင်ချက်က သင့်အား ပြောပြလိမ့်မည် ။
ဥပမာ
string တစ်ခုရှိ "w3schools" အတွက် case-sensitive ရှာဖွေမှုပြုလုပ်ရန် ပုံမှန်အသုံးအနှုန်းကိုသုံးပါ-
<?php
$str = "Visit W3Schools";
$pattern = "/w3schools/i";
echo
preg_match($pattern, $str); // Outputs 1
?>
preg_match_all() ကိုအသုံးပြုခြင်း
လုပ်ဆောင်ချက် သည် preg_match_all()
string တစ်ခုတွင် ပုံစံတစ်ခုအတွက် ကိုက်ညီမှုမည်မျှရှိသည်ကို သင့်အား ပြောပြလိမ့်မည်။
ဥပမာ
စာကြောင်းတစ်ခုရှိ "ain" ၏ဖြစ်ပျက်မှုအရေအတွက်၏ case-insensitive count ကိုပြုလုပ်ရန် ပုံမှန် expression ကိုသုံးပါ-
<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern = "/ain/i";
echo preg_match_all($pattern, $str); // Outputs 4
?>
preg_replace() ကိုအသုံးပြုခြင်း
လုပ်ဆောင်ချက်သည် စာကြောင်းတစ်ခုရှိ ပုံစံ ၏ preg_replace()
ကိုက်ညီမှုအားလုံးကို အခြားစာကြောင်းတစ်ခုဖြင့် အစားထိုးမည်ဖြစ်သည်။
ဥပမာ
Microsoft ကို W3Schools ဖြင့် အစားထိုးရန် စာကြောင်းတစ်ခုတွင် case-insensitive ပုံမှန် expression ကိုသုံးပါ-
<?php
$str = "Visit Microsoft!";
$pattern = "/microsoft/i";
echo
preg_replace($pattern, "W3Schools", $str); // Outputs "Visit
W3Schools!"
?>
ပုံမှန်ဖော်ပြချက်မွမ်းမံမှုများ
မွမ်းမံပြင်ဆင်သူများသည် ရှာဖွေမှုလုပ်ဆောင်ပုံကို ပြောင်းလဲနိုင်သည်။
Modifier | Description |
---|---|
i | Performs a case-insensitive search |
m | Performs a multiline search (patterns that search for the beginning or end of a string will match the beginning or end of each line) |
u | Enables correct matching of UTF-8 encoded patterns |
ပုံမှန်ဖော်ပြမှုပုံစံများ
စာလုံးအကွာအဝေးကိုရှာရန် ကွင်းပိတ်များကိုအသုံးပြုသည်-
Expression | Description |
---|---|
[abc] | Find one character from the options between the brackets |
[^abc] | Find any character NOT between the brackets |
[0-9] | Find one character from the range 0 to 9 |
Metacharacters
Metacharacters များသည် အထူးအဓိပ္ပါယ်ရှိသော ဇာတ်ကောင်များဖြစ်သည်-
Metacharacter | Description |
---|---|
| | Find a match for any one of the patterns separated by | as in: cat|dog|fish |
. | Find just one instance of any character |
^ | Finds a match as the beginning of a string as in: ^Hello |
$ | Finds a match at the end of the string as in: World$ |
\d | Find a digit |
\s | Find a whitespace character |
\b | Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b |
\uxxxx | Find the Unicode character specified by the hexadecimal number xxxx |
အရေအတွက်
Quantifiers သည် ပမာဏများကို သတ်မှတ်သည်-
Quantifier | Description |
---|---|
n+ | Matches any string that contains at least one n |
n* | Matches any string that contains zero or more occurrences of n |
n? | Matches any string that contains zero or one occurrences of n |
n{x} | Matches any string that contains a sequence of X n's |
n{x,y} | Matches any string that contains a sequence of X to Y n's |
n{x,} | Matches any string that contains a sequence of at least X n's |
မှတ်ချက်- သင့်ဖော်ပြချက်သည် အထူးဇာတ်ကောင်များထဲမှ တစ်ခုကို ရှာဖွေရန် လိုအပ်ပါက ၎င်းတို့ကို လွတ်မြောက်ရန် backslash ( \ ) ကို အသုံးပြုနိုင်သည်။ ဥပမာအားဖြင့်၊ တစ်ခု သို့မဟုတ် တစ်ခုထက်ပိုသော မေးခွန်းအမှတ်အသားများကို ရှာဖွေရန် အောက်ပါအချက်များကို သင်သုံးနိုင်သည်- $pattern = '/\?+/';
အုပ်စုဖွဲ့ခြင်း။
( )
ပုံစံများတစ်ခုလုံးတွင် quantifiers များကို အသုံးပြုရန် ကွင်း အတွင်းကို အသုံးပြုနိုင်သည်။ ကိုက်ညီမှုအဖြစ် အသုံးပြုမည့် ပုံစံ၏ အစိတ်အပိုင်းများကို ရွေးချယ်ရန် ၎င်းတို့ကိုလည်း အသုံးပြုနိုင်သည်။
ဥပမာ
ba ကို ရှာဖွေခြင်းဖြင့် "ငှက် ပျော " ဟူသောစကားလုံးကိုရှာဖွေရန် အုပ်စုဖွဲ့ခြင်းကိုသုံး ပါ
<?php
$str = "Apples and bananas.";
$pattern = "/ba(na){2}/i";
echo
preg_match($pattern, $str); // Outputs 1
?>
RegExp အကိုးအကား အပြည့်အစုံ
အပြည့်အစုံကိုးကားရန်အတွက်၊ ကျွန်ုပ်တို့၏ ပြီးပြည့်စုံသော PHP Regular Expression Reference သို့ သွားပါ ။
ရည်ညွှန်းချက်တွင် Regular Expression လုပ်ဆောင်ချက်အားလုံး၏ ဖော်ပြချက်များနှင့် ဥပမာများ ပါရှိသည်။