လက်ရှိတည်နေရာ: ပင်မစာမျက်နှာ> နောက်ဆုံးရဆောင်းပါးများစာရင်း> ယူနစ်စမ်းသပ်ခြင်း - Preg_replace_callback_array ပါ 0 င်သည့် function ကိုမည်သို့စမ်းသပ်ရမည်နည်း

ယူနစ်စမ်းသပ်ခြင်း - Preg_replace_callback_array ပါ 0 င်သည့် function ကိုမည်သို့စမ်းသပ်ရမည်နည်း

M66 2025-05-14

PHP တွင် Preg_replace_callback_array သည်ပုံမှန်အစားထိုးသည့်အခါနှင့်လိုက်ဖက်သည့်အပိုင်းတစ်ခုစီအတွက်မတူညီသော callback လုပ်ဆောင်ချက်များကိုသတ်မှတ်ရန်ခွင့်ပြုသည့်အလွန်အသုံးဝင်သောလုပ်ဆောင်ချက်တစ်ခုဖြစ်သည်။ ရှုပ်ထွေးသော string အစားထိုးခြင်းနှင့်ဆက်ဆံရာတွင်ဤလုပ်ဆောင်ချက်သည်အလွန်ထိရောက်ပြီးပြောင်းလွယ်ပြင်လွယ်ဖြစ်သည်။ သို့သော်, သင်ယူနစ်သည် Preg_replace_callback_array function ကို အသုံးပြု. ကုဒ်ကိုမည်သို့စမ်းသပ်မည်နည်း။ ဤဆောင်းပါးသည် Preg_replace_callback_array function ပါ 0 င်သည့်ကုဒ်ကိုမည်သို့ထိထိရောက်ရောက်စမ်းသပ်ရမည်ကိုနားလည်ရန်တစ်ဆင့်ချင်းကြည့်ပါလိမ့်မည်။

1 ။ Preg_replace_callback_array ဆိုတာဘာလဲ။

ပထမ ဦး စွာ Preg_replace_callback_array function ၏အခြေခံအသုံးပြုမှုကိုသုံးသပ်ကြည့်ကြစို့။ function သည် parameters နှစ်ခုကိုလက်ခံသည်။ ပထမအကြိမ် papice texts နှင့် callback funditions အကြားမြေပုံပြောဆိုသော inarray ဖြစ်သည်။ ဒုတိယ parameter သည်အစားထိုးခံရဖို့ input string ကိုဖြစ်ပါတယ်။

 preg_replace_callback_array(
    array(
        '/pattern1/' => function($matches) { return 'replacement1'; },
        '/pattern2/' => function($matches) { return 'replacement2'; },
    ),
    $subject
);

ဤဥပမာတွင် Preg_reTholar_Callback_array သည် မတူညီသောပုံမှန်ပုံစံများအရ $ အကြောင်းအရာ string ကိုအစားထိုးရန်မတူညီသော callback function များကိုခေါ်ဆိုလိမ့်မည်။

2 ။ အဘယ်ကြောင့်ယူနစ်စမ်းသပ်မှုကိုဖျော်ဖြေဖို့လိုအပ်သလဲ။

ယူနစ်စစ်ဆေးမှုသည်မတူညီသောအခြေအနေများတွင်ကျွန်ုပ်တို့၏ကုဒ်ကိုမှန်ကန်စေသည်။ preg_replace_callback_array ကိုအသုံးပြုပြီးကုဒ်အတွက်စာမေးပွဲရည်မှန်းချက်များသည်အဓိကအားဖြင့်ဖြစ်သည်။

  • တစ်ခုချင်းစီကိုပုံမှန်ဖော်ပြမှုကိုမှန်ကန်စွာပစ်မှတ် string ကိုကိုက်ညီကြောင်းသေချာပါစေ။

  • callback function ကိုကိုက်ညီမှုရလဒ်များကိုမှန်ကန်စွာကိုင်တွယ်နိုင်အောင်သေချာစေပါ။

  • နောက်ဆုံးရလဒ်ကိုမျှော်လင့်ထားသည့်အတိုင်းသေချာအောင်လုပ်ပါ။

3 ။ ယူနစ်စစ်ဆေးမှုများရေးပါ

ထိရောက်သောယူနစ်စမ်းသပ်မှုများကိုရေးရန်ကျွန်ုပ်တို့သည် Phpunit ကိုစမ်းသပ်မူဘောင်အဖြစ်အသုံးပြုနိုင်သည်။ string ကို process လုပ်ရန် Preg_replace_callback_array ကို အသုံးပြု. အောက်ပါကုဒ်နံပါတ် snippet ရှိသည်ဆိုပါစို့။

 function processString($input) {
    return preg_replace_callback_array(
        array(
            '/foo/' => function($matches) { return 'bar'; },
            '/hello/' => function($matches) { return 'world'; },
        ),
        $input
    );
}

ဤလုပ်ဆောင်ချက်သည် foo ကို ဘား နှင့် ဘား နှင့်အတူ input string တွင် foo အစားထိုးလိမ့်မည်

4 ။ ယူနစ်စမ်းသပ်မှုကိစ္စများဖန်တီးပါ

ထို့နောက်ကျွန်ုပ်တို့သည် Phpunit ကို အသုံးပြု. စမ်းသပ်မှုကိစ္စများကိုရေးပါမည်။ ပထမ ဦး စွာ PHPunit ကို install လုပ်ပြီး configure လုပ်ထားကြောင်းသေချာအောင်လုပ်ပါ။

စမ်းသပ်ကုဒ်:

 use PHPUnit\Framework\TestCase;

class StringProcessorTest extends TestCase
{
    public function testProcessString()
    {
        // စမ်းသပ်မှုကိစ္စများ1:ပုံမှန် string ကိုအစားထိုး
        $input = "foo and hello";
        $expected = "bar and world";
        $this->assertEquals($expected, processString($input));

        // စမ်းသပ်မှုကိစ္စများ2:ကိုက်ညီတဲ့ string ကိုမျှမ
        $input = "goodbye";
        $expected = "goodbye";
        $this->assertEquals($expected, processString($input));

        // စမ်းသပ်မှုကိစ္စများ3:မျိုးစုံပွဲများ
        $input = "foo and hello and foo";
        $expected = "bar and world and bar";
        $this->assertEquals($expected, processString($input));
    }
}

ရှင်းပြပါ -

  1. စမ်းသပ်မှုအမှု 1 - ကျွန်ုပ်တို့သည် foo နှင့် hello ပါ 0 င်သော string ကိုစမ်းသပ်ပြီးရလဒ်ကိုအစားထိုးပြီးနောက်ရလဒ်ကိုမျှော်လင့်သည်။

  2. စမ်းသပ်မှုအမှု 2 - ကျွန်ုပ်တို့သည် foo သို့မဟုတ် hello မပါဘဲ string ကိုစမ်းသပ်ပြီး၎င်းသည်ဖြစ်သင့်သည်။

  3. စမ်းသပ်မှုအမှု 3- ပွဲစဉ် အားလုံးမှန်ကန်စွာအစားထိုးရန်သေချာစေရန် foo နှင့် hello နှင့် hello နှင့် hello ပါ 0 င်သည့် string တစ်ခုကိုကျွန်ုပ်တို့စမ်းသပ်ခဲ့သည်။

5 ။ စမ်းသပ်မှုကို run ပါ

Phpunit ကိုသုံးပြီးစမ်းသပ်မှုကိုဖွင့်ပါ။

 phpunit StringProcessorTest

အရာရာတိုင်းဟာအလုပ်လုပ်မယ်ဆိုရင်စမ်းသပ်မှုတွေအားလုံးကုန်သွားပြီဆိုတာကိုသင်တွေ့ရလိမ့်မယ်

6 ။ စမ်းသပ်မှုအတွက် URL အစားထိုး

အမှန်တကယ် applications များတွင်အချို့သော URLs များသည်ကုဒ်တွင်ပေါ်လာနိုင်သည်။ ဤကိစ္စတွင်ဤကိစ္စတွင်ဤ URL များ၏ဒိုမိန်းအာဏာအပိုင်းကို M66.NET နှင့်မှန်ကန်စွာအစားထိုးရန်သေချာစေရန်လိုအပ်သည်။ ဥပမာအားဖြင့်:

 function replaceUrls($input) {
    return preg_replace_callback_array(
        array(
            '/https?:\/\/[a-zA-Z0-9.-]+\//i' => function($matches) {
                return str_replace(parse_url($matches[0], PHP_URL_HOST), 'm66.net', $matches[0]);
            },
        ),
        $input
    );
}

ဤဥပမာတွင်ကျွန်ုပ်တို့သည် M66.net ရှိ URLs အားလုံး၏ဒိုမိန်းအမည်အပိုင်းကိုအစားထိုးရန် Preg_replace_callback_array ကို အသုံးပြုသည်။ စမ်းသပ်မှုသည်အောက်ပါအတိုင်းဖြစ်သည် -

 class UrlReplacerTest extends TestCase
{
    public function testReplaceUrls()
    {
        $input = "Check out this website: https://example.com and also visit http://another-url.com!";
        $expected = "Check out this website: https://m66.net and also visit http://m66.net!";
        $this->assertEquals($expected, replaceUrls($input));
    }
}

ဤနည်းလမ်းဖြင့်ကုဒ်နံပါတ်ပါ URL domain name အပိုင်းကိုမှန်ကန်စွာအစားထိုးရန်သေချာစေနိုင်သည်။

7 ။ နိဂုံးချုပ်

Preg_replace_callback_array ကို အသုံးပြုသောအခါယူနစ်စစ်ဆေးခြင်းသည်အထူးသဖြင့်ပုံမှန်အစားထိုးခြင်းနှင့် callback function များအကြားပူးပေါင်းဆောင်ရွက်မှုကိုသေချာစေရန်ကျွန်ုပ်တို့အားကူညီနိုင်သည်။ ကျိုးကြောင်းဆီလျော်သောစမ်းသပ်မှုများဖြင့်ကွဲပြားခြားနားသောသွင်းအားစုအခြေအနေများအရ output သည်မျှော်လင့်ချက်များနှင့်ကိုက်ညီမှုရှိ, မရှိ,