PHP တွင် PREG_RESTER_CALLATER_CALLALLBAK_ARRAY သည်ပုံမှန်စကားရပ်မျိုးစုံကွဲပြားခြားနားသောအကြောင်းအရာနှင့်ကိုက်ညီသည့်အခါပွဲတိုင်းကိုကိုင်တွယ်ရန်မတူညီသော callback function များကိုအသုံးပြုရန်ခွင့်ပြုသည်။ ၎င်း၏ function ကိုပုံမှန်ပုံစံများနှင့်၎င်းတို့၏သက်ဆိုင်ရာ callback functack ကို Associative ခင်းကျင်းအဖြစ်ဖြတ်သန်းရန်နှင့် string ကိုအစားထိုးဖျော်ဖြေသောအခါကိုက်ညီသောပုံမှန်စကားရပ်အရသက်ဆိုင်ရာ callback လုပ်ဆောင်ချက်များကိုလုပ်ဆောင်ရန်။
သို့သော် Preg_replace_CAllback_array ကို အသုံးပြုသောအခါ, သင်သည်ပုံမှန်ပုံစံများအမိန့်ဖြစ်သည့်ပြ a နာတစ်ခုနှင့်ကြုံတွေ့ရနိုင်သည်။ အကယ်. ကျွန်ုပ်တို့သည်ပုံမှန်အသုံးအနှုန်းကိုမှန်ကန်သောအမိန့်ဖြင့်မရေးပါက၎င်းတို့သည်မျှော်လင့်ထားသည့်အတိုင်းတစ်စိတ်တစ်ပိုင်းအစားထိုးလုပ်ငန်းများကိုမလုပ်ဆောင်နိုင်ပါ။ သို့မဟုတ်အချို့သောပွဲများကိုအမှားများဖြင့်အစားထိုးသည်။ ဤဆောင်းပါးသည်ပုံမှန်ပုံစံအမိန့်ကြောင့်ဖြစ်ပေါ်လာသောပြ problems နာများကိုဖြေရှင်းရန် Preg_replace_callback_array ကို မည်သို့ဖြေရှင်းရမည်ကိုမိတ်ဆက်ပေးလိမ့်မည်။
ပထမဥပမာ, ရိုးရှင်းသောဥပမာကိုကြည့်ကြစို့။
<?php
$string = "Visit our website at http://example.com or http://example.org for more information.";
$patterns = [
'/http:\/\/example\.com/' => function ($matches) {
return 'http://m66.net';
},
'/http:\/\/example\.org/' => function ($matches) {
return 'http://m66.net';
},
];
$result = preg_replace_callback_array($patterns, $string);
echo $result;
?>
ဤဥပမာတွင် URLs နှစ်ခုကို http://exampe.com နှင့် http://xampp://xample.org နှင့် http://m66.net ဖြင့်အစားထိုးပါ။ ဒီကုဒ်ကို run နှင့် output ကို:
Visit our website at http://m66.net or http://m66.net for more information.
အဆိုပါပြ problem နာကိုပုံမှန်ပုံစံများအမိန့်တွင်တွေ့ရှိနိုင်ပါသည်။ အကယ်. ကျွန်ုပ်တို့သည်ပုံမှန်အသုံးအနှုန်းနှစ်ခု၏အမိန့်ကိုလဲလှယ်လျှင်၎င်းသည်ရလဒ်ကိုမျှော်လင့်ချက်များနှင့်မကိုက်ညီပါစေနိုင်သည်။ ဥပမာအားဖြင့်:
<?php
$string = "Visit our website at http://example.com or http://example.org for more information.";
$patterns = [
'/http:\/\/example\.org/' => function ($matches) {
return 'http://m66.net';
},
'/http:\/\/example\.com/' => function ($matches) {
return 'http://m66.net';
},
];
$result = preg_replace_callback_array($patterns, $string);
echo $result;
?>
ဤဗားရှင်းတွင်ဤဗားရှင်းတွင်ပုံမှန်ပုံစံနှစ်ခု၏အမိန့်ကိုလဲလှယ်သည်။ ကုဒ်နံပါတ်ကိုဖွင့်တဲ့အခါမှာ output ဟာ -
Visit our website at http://m66.net or http://m66.net for more information.
ရလဒ်သည်ပြောင်းလဲခြင်းမရှိသေးသော်လည်း Regex သည်ပိုမိုရှုပ်ထွေးလာပါက (ထိုကဲ့သို့သော optionaled optional ပုံစံမျိုးစုံရှိခြင်းကဲ့သို့သော) အမိန့်သည်အလွန်အရေးကြီးသည်။ ဥပမာအားဖြင့်ကျွန်ုပ်တို့သည်အထူး URLs အချို့နှင့်ကိုက်ညီပြီးတန်ဖိုးအမျိုးမျိုးဖြင့်အစားထိုးရန်လိုသည်ဆိုပါစို့။ အကယ်. အမိန့်သည်မမှန်ကန်ပါကအချို့သောပွဲများကိုလွဲချော်နိုင်သည်။
ဤပြ problem နာကိုရှောင်ရှားရန်ကျွန်ုပ်တို့သည်ပုံမှန်အသုံးအနှုန်းများအမိန့်ကိုချိန်ညှိရန်လိုအပ်သည်။ အဖြေသည်ပုံမှန်အသုံးအနှုန်းများအရအမိန့်ကိုဂရုတစိုက်ဒီဇိုင်းရေးဆွဲခြင်းသို့မဟုတ်ပုံမှန်ဖော်ပြမှုတစ်ခုစီတွင်လွတ်လပ်သောအစားထိုးလုပ်ငန်းများလုပ်ဆောင်ခြင်းအားဖြင့်အဖြေရှာခြင်းကိုရှောင်ရှားနိုင်သည်။ ဥပမာအားဖြင့်ကျွန်ုပ်တို့သည်အထူး URLs အချို့ကို ဦး စွာလုပ်ဆောင်နိုင်ပြီးဘုံ urls များကိုပြုလုပ်နိုင်သည်။
ကွဲပြားခြားနားသော URL နှစ်ခုကိုသီးခြားအစားထိုးရန်လိုအပ်ကြောင်းကျွန်ုပ်တို့ပိုမိုရှုပ်ထွေးသောလိုအပ်ချက်ရှိသည်ဆိုပါစို့။
အတိအကျကိုက်ညီသော URL : ကျွန်ုပ်တို့သည် http://exple.com ကို ပထမ ဦး စွာပြုလုပ်ပါ။
Pan-Match URL : နောက်တစ်ခုကအခြား URLs အမျိုးအစားများ ( http://example.org ) ကဲ့သို့သောအခြား URL အမျိုးအစားများကိုလုပ်ဆောင်ပါ။
<?php
$string = "Visit our website at http://example.com, http://example.org, and http://example.net for more information.";
$patterns = [
'/http:\/\/example\.com/' => function ($matches) {
return 'http://m66.net';
},
'/http:\/\/example\.org/' => function ($matches) {
return 'http://m66.net';
},
'/http:\/\/example\.net/' => function ($matches) {
return 'http://m66.net';
},
];
$result = preg_replace_callback_array($patterns, $string);
echo $result;
?>
ဤကုဒ်သည် http://example.com , http://xample.org နှင့် http://example.net ကို http://m66.net သို့အစားထိုးလိမ့်မည်။
ReG_replace_callback_Accallback_array ကိုအသုံးပြုသောအခါပုံမှန်ပုံစံများအမိန့်သည်အလွန်အရေးကြီးသည်။ အကယ်. အမိန့်မမှန်ကန်ပါကအချို့အစားထိုးမှုများကျရှုံးလိမ့်မည်။ ပုံမှန်ပုံစံများကိုစနစ်တကျဒီဇိုင်းရေးဆွဲခြင်းသို့မဟုတ်ပုံမှန်ဖော်ပြမှုတစ်ခုစီကိုလွတ်လပ်သောအရပြ issues နာများနှင့်ပတ်သက်သောအမှားများကိုရှောင်ရှားနိုင်သည်။ ရှုပ်ထွေးသောပုံမှန်အစားထိုးလုပ်ငန်းများကိုပိုမိုလွယ်ကူစွာကိုင်တွယ်ရန်ကူညီရန် Preg_replace_callback_array ၏အစွမ်းထက်သောလက္ခဏာများကိုနားလည်ပြီးအသုံးပြုပါ။