PHP ပရိုဂရမ်တွင်ကျွန်ုပ်တို့သည်ရှုပ်ထွေးသောကိုက်ညီမှုနှင့်ကြိုးများပေါ်တွင်အစားထိုးလုပ်ဆောင်မှုများပြုလုပ်ရန်လိုအပ်သည်။ PREG_MATCH_ALLE_CASTER_CARTLOTER_CARLESTER_ARALBAKE_ARRAY သည်ဤလုပ်ဆောင်မှု ကို ကျွန်ုပ်တို့အားဤလုပ်ဆောင်မှုကိုအကောင်အထည်ဖော်ရန်အတွက်အထူးသဖြင့်ရှုပ်ထွေးသောပုံစံနှင့်အစားထိုးခြင်းနှင့်အစားထိုးခြင်းတို့ကိုကိုင်တွယ်ဖြေရှင်းနိုင်သည့်အလွန်အသုံးဝင်သောလုပ်ဆောင်ချက်နှစ်ခုဖြစ်သည်။ ဤဆောင်းပါးသည်ဤလုပ်ဆောင်ချက်နှစ်ခုကိုပေါင်းစပ်ပြီးမည်ကဲ့သို့အသုံးပြုရမည်ကိုအသေးစိတ်ဖော်ပြရန်နှင့်ထုတ်ယူခြင်း၏လုပ်ငန်းများကိုမည်သို့အကောင်အထည်ဖော်ရန်နှင့်၎င်းတို့အားဖြင့်အစားထိုးခြင်းများကိုမည်သို့အကောင်အထည်ဖော်ရမည်ကိုအသေးစိတ်ဖော်ပြထားသည်။
PREG_MATCH_ALL သည် PHP တွင်အသုံးပြုသော function တစ်ခုဖြစ်သည်။ ၎င်းသည် string ကိုစကင်ဖတ်စစ်ဆေးပြီးပုံမှန်စကားရပ်နှင့်ကိုက်ညီသောပွဲစဉ်အားလုံးကိုပြန်လည်ရယူသည်။ ဤလုပ်ဆောင်မှုမှရရှိသောရလဒ်သည်များသောအားဖြင့်ကိုက်ညီသောကြိုးများအားလုံးပါ 0 င်သော Multidimensional Warray ဖြစ်သည်။
$pattern = '/\bhttps?:\/\/[a-zA-Z0-9-]+\.[a-zA-Z0-9-]+\b/';
$string = "Visit https://example.com or http://test.com for more information.";
preg_match_all($pattern, $string, $matches);
print_r($matches);
အထက်ပါကုဒ်သည် string အတွက် urls အားလုံးနှင့်ကိုက်ညီလိမ့်မည်။
Preg_replace_callback_array သည်ပုံမှန်အသုံးအနှုန်းမျိုးစုံမျိုးစုံနှင့်ကိုက်ညီစေရန်နှင့်ပုံမှန်စကားရပ်တစ်ခုစီအတွက်မတူညီသော callback function များကိုသတ်မှတ်ရန်ခွင့်ပြုသည့်အစွမ်းထက်သော function တစ်ခုဖြစ်သည်။ ဤသည်ပြောင်းလွယ်ပြင်လွယ် string ကိုအစားထိုးဘို့ခွင့်ပြုပါတယ်။
$patterns = [
'/\d+/' => function($matches) {
return $matches[0] * 2; // နံပါတ်များကိုမြှောက်ပါ2
},
'/[a-zA-Z]+/' => function($matches) {
return strtoupper($matches[0]); // စာလုံးများကိုစာလုံးအဖြစ်ပြောင်းပါ
}
];
$string = "The quick 3 brown 5 foxes";
echo preg_replace_callback_array($patterns, $string);
ဤကုဒ်သည် strings နံပါတ်များကိုမြှောက်ပေးပြီးစာလုံးများကိုစာလုံးအမြောက်အများသို့ပြောင်းလဲလိမ့်မည်။
လက်တွေ့ကျသောအပလီကေးရှင်းများတွင်ကျွန်ုပ်တို့သည်တိကျသောပါဝင်မှုအချို့ကို ဦး စွာထုတ်ယူရန်လိုအပ်ပြီးလိုအပ်ပါကအစားထိုးရန်လိုအပ်သည်။ ဤလုပ်ဆောင်ချက်သည် Preg_match_all နှင့် Preg_replace_callback_array ကို ပေါင်းစပ်ခြင်းဖြင့်အလွယ်တကူအကောင်အထည်ဖော်နိုင်သည်။
ကျွန်ုပ်တို့သည် URL အားလုံးကိုစာသားတစ်ခုမှထုတ်ယူပြီး M66.net ရှိ domain names များကိုအစားထိုးရန်လိုအပ်သည်ဆိုပါစို့။
ပထမ ဦး စွာ URL အားလုံးကို Extract လုပ်ရန် Preg_Match_All ကို သုံးနိုင်သည်။
$pattern = '/https?:\/\/([a-zA-Z0-9-]+\.[a-zA-Z0-9-]+)/';
$string = "Check out https://example.com and http://test.com for more information.";
preg_match_all($pattern, $string, $matches);
print_r($matches);
ဤကုဒ်သည် URL domain name name အစိတ်အပိုင်းများအားလုံးကိုထုတ်ယူ သည် ။
ထို့နောက်ကျွန်ုပ်တို့သည် Extracted domain name အပိုင်းကိုအစားထိုးရန် Preg_replace_callback_array ကို အသုံးပြုသည်။ M66.net ဖြင့် callback function တွင် domain name ကိုကျွန်ုပ်တို့အစားထိုးနိုင်သည်။
$patterns = [
'/https?:\/\/([a-zA-Z0-9-]+\.[a-zA-Z0-9-]+)/' => function($matches) {
// အစားထိုးလိုက်ပါ m66.net ဒိုမိန်းအမည်
return str_replace($matches[1], 'm66.net', $matches[0]);
}
];
$string = "Check out https://example.com and http://test.com for more information.";
$result = preg_replace_callback_array($patterns, $string);
echo $result;
ဤကုဒ်သည်စာသားအတွင်းရှိ URL domain name အားလုံးကို m66.net ဖြင့်အစားထိုးလိမ့်မည်။ ဥပမာ - https://xpleck.com https://m66.net ဖြင့်အစားထိုးလိမ့်မည်။
Here is a complete example code combining preg_match_all and preg_replace_callback_array , which is able to extract the URL and replace the domain name in it as m66.net :
<?php
// အားလုံးကိုထုတ်ယူပါ URL 并替换ဒိုမိန်းအမည်
$pattern = '/https?:\/\/([a-zA-Z0-9-]+\.[a-zA-Z0-9-]+)/';
$string = "Check out https://example.com and http://test.com for more information.";
// ထုတ်ပြန် URL
preg_match_all($pattern, $string, $matches);
print_r($matches);
// 替换ဒိုမိန်းအမည်
$patterns = [
'/https?:\/\/([a-zA-Z0-9-]+\.[a-zA-Z0-9-]+)/' => function($matches) {
return str_replace($matches[1], 'm66.net', $matches[0]);
}
];
// string ကိုအစားထိုးလိုက်ပါ URL ဒိုမိန်းအမည်
$result = preg_replace_callback_array($patterns, $string);
echo $result;
?>
Preg_Match_all နှင့် Preg_replace_callback_Array ကို ပေါင်းစပ်ခြင်းအားဖြင့်ကျွန်ုပ်တို့သည်လိုအပ်သော strings content ကိုပထမဆုံးထုတ်ယူပြီးထိုအကြောင်းအရာများတွင်ထုံးစံအတိုင်းအစားထိုးခြင်းလုပ်ငန်းများကိုလုပ်ဆောင်နိုင်သည်။ ဤနည်းလမ်းသည်အလွန်ပြောင်းလွယ်ပြင်လွယ်ရှိပြီးရှုပ်ထွေးသော string processing လုပ်ငန်းတာဝန်အမျိုးမျိုးကိုလျှောက်ထားနိုင်သည်။
စာသားများကိုပုံမှန်ဖော်ပြရန်ပုံမှန်အသုံးအနှုန်းများကိုအသုံးပြုသောအခါကျွန်ုပ်တို့သည်ပိုမိုများပြားသောထုတ်ယူခြင်းနှင့်အစားထိုးလုပ်ငန်းများကိုပိုမိုထိရောက်စွာဖြည့်ဆည်းနိုင်ပြီးအထူးသဖြင့် URLs များနှင့်အီးမေးလ်လိပ်စာများကဲ့သို့သောပုံသေသောပုံစံများဖြင့်စာသားများကိုပြုပြင်ပြောင်းလဲနိုင်သည်။