PHP တွင် striviing function ကိုများသောအားဖြင့်အခြား string တစ်ခုတွင် string တစ်ခုတွင်ပထမ ဦး ဆုံးပေါ်လာသည့်နေရာကိုရှာဖွေရန်အသုံးပြုသည်။ အကယ်. သင်သည် String တစ်ခုတွင်ကမ္ဘာလုံးဆိုင်ရာရှာဖွေမှုကိုပြုလုပ်လိုပါက strips များသည် တိုက်ရိုက်ပံ့ပိုးမှုပုံစံမဟုတ်ပါ,
ဒါကြောင့် Global Search ကိုအကောင်အထည်ဖော်ဖို့ Stripos ကို ဘယ်လိုတုပသင့်သလဲ။ ဤဆောင်းပါးသည်ဤပြ issue နာကိုလေ့လာပြီးအလားတူလုပ်ဆောင်မှုကိုအကောင်အထည်ဖော်ရန်အခြားနည်းလမ်းအချို့ကိုဖော်ပြပေးလိမ့်မည်။
Global Search ကိုတုပရန်ကျွန်ုပ်တို့သည် string အပြည့်အဝမရှာဖွေသည်အထိနောက်လိုက်ဖက်သည့်အနေအထားကိုအဆက်မပြတ်ရှာဖွေခြင်းဖြင့်ပေါင်းစပ်ရန် strips နှင့် substrts ကို သုံးနိုင်သည်။
ဤတွင် Global Search ကို Simulates Code Pegion:
<?php
function globalStripos($haystack, $needle) {
$offset = 0; // string ကို string ကိုစတင်ရန်စတင်ရှာဖွေပါ
$matches = []; // အားလုံးကိုက်ညီသောတည်နေရာများကိုကယ်တင်ရန်အသုံးပြုခဲ့သည်
while (($pos = stripos($haystack, $needle, $offset)) !== false) {
$matches[] = $pos;
$offset = $pos + 1; // လက်ရှိအနေအထားတွင်နောက်ဇာတ်ကောင်မှဆက်ရှာဖွေပါ
}
return $matches;
}
$text = "This is a test string. Let's test stripos function.";
$needle = "test";
$result = globalStripos($text, $needle);
print_r($result); // အားလုံးကိုက်ညီရာထူး output
?>
ဤဥပမာတွင် GlobalStosTos function သည်ပွဲတစ်ခုစီ၏အနေအထားကိုရှာဖွေရန် strips များကို အသုံးပြုသည်။ နောက်ဆုံးလိုက်ဖက်သည့်အနေအထား၏နောက်ဇာတ်ကောင်မှစတင်သောအခါတိုင်းကိုက်ညီသောရာထူးများအားလုံးကို striars နှင့် returns များအားလုံးကိုသိုလှောင်ထားသည့်အတွက် Offset Parameter ကိုချိန်ညှိသည်။
အကယ်. သန့်ရှင်းသောဖြေရှင်းနည်းကိုသင်အသုံးပြုလိုပါကပုံမှန်အသုံးအနှုန်း၏ preg_match_all function ကိုအသုံးပြုပါ။ ဤလုပ်ဆောင်ချက်သည်ပွဲအားလုံး၏တည်နေရာအချက်အလက်ကိုပြန်ပို့ပြီးပိုမိုရှုပ်ထွေးသောပွဲများအတွက်ခွင့်ပြုသည်။
Preg_match_all ကို အသုံးပြု. Global Search ကိုအကောင်အထည်ဖော်ရန်အတွက်နမူနာကုဒ်ဖြစ်သည်။
<?php
function globalSearchWithPreg($haystack, $needle) {
$pattern = '/' . preg_quote($needle, '/') . '/i'; // ပုံမှန်အသုံးအနှုန်းများကိုအသုံးပြုခြင်း,'i' အလံများအမှုအာရုံမခံစားနိုင်ကြောင်းညွှန်ပြ
preg_match_all($pattern, $haystack, $matches, PREG_OFFSET_CAPTURE);
$positions = [];
foreach ($matches[0] as $match) {
$positions[] = $match[1]; // ကိုက်ညီသောတည်နေရာကိုရယူပါ
}
return $positions;
}
$text = "This is a test string. Let's test preg_match_all function.";
$needle = "test";
$result = globalSearchWithPreg($text, $needle);
print_r($result); // အားလုံးကိုက်ညီရာထူး output
?>
ဤဥပမာတွင် Preg_match_all အားလုံးသည် ပွဲအားလုံးပါ 0 င်သောခင်းကျင်းမှုကိုပြန်လည်ရောက်ရှိစေသည်။ preg_offset_Capture parameter သည် preg_match_all သည် ပွဲ၏တည်နေရာအချက်အလက်ကိုပြန်ပို့ပေးလိမ့်မည်။
အကယ်. သင်သည်ပုံမှန်အသုံးအနှုန်းများကိုမသုံးလိုပါက strstrs သို့မဟုတ် strpos များသည် အလားအလာရှိသောအခြားနည်းလမ်းများဖြစ်သည်။ ဤလုပ်ဆောင်မှုများသည်ပထမအကြိမ်ဖြစ်စဉ်ကိုရှာဖွေရန်ပုံမှန်အားဖြင့်ဖြစ်သော်လည်း Search Start ကိုချိန်ညှိခြင်းဖြင့်ကမ္ဘာလုံးဆိုင်ရာရှာဖွေမှုကိုတုပနိုင်သည်။
Strstr . ကိုအသုံးပြုပြီးဥပမာတစ်ခုရှိသည်။
<?php
function globalSearchWithStrstr($haystack, $needle) {
$offset = 0;
$matches = [];
while (($pos = strstr(substr($haystack, $offset), $needle)) !== false) {
$matches[] = $pos;
$offset += strlen($pos) - strlen($needle) + 1; // လာမယ့်တည်နေရာကနေဆက်ရှာဖွေပါ
}
return $matches;
}
$text = "This is a test string. Let's test strstr function.";
$needle = "test";
$result = globalSearchWithStrstr($text, $needle);
print_r($result); // အားလုံးကိုက်ညီ substring အားလုံး output
?>
ဤနည်းလမ်းသည်ယခင် stripos simulation နှင့်ဆင်တူသည့်ပွဲတစ်ခုစီကိုရှာဖွေရန် stravs ကို အသုံးပြုသည်။
Preg_Match_All ကို ပုံမှန်ကိုက်ညီစေရန်သို့မဟုတ် stripst outs and strippos များကို အသုံးပြုခြင်း, နည်းလမ်းတစ်ခုစီတွင်၎င်း၏အားသာချက်များနှင့်အားနည်းချက်များရှိသည်။ သင်၏မြင်ကွင်းနှင့်ကိုက်ညီသောအဖြေရှာရန်အလွန်အရေးကြီးသည်။ ပိုမိုရှုပ်ထွေးသောကိုက်ညီသည့်ယုတ္တိဗေဒသို့မဟုတ်ပြောင်းလွယ်ပြင်လွယ်ပုံစံကိုက်ညီမှုရှိပါကပုံမှန်အသုံးအနှုန်းများကိုအသုံးပြုခြင်းသည်များသောအားဖြင့်အကောင်းဆုံးရွေးချယ်မှုဖြစ်သည်။