Stropos ၏အသုံးပြုမှုသည်ရိုးရှင်းပြီးရှင်းလင်းသည်။
<?php
$haystack = "hello world, hello php";
$needle = "hello";
$pos = strpos($haystack, $needle);
echo $pos; // ထုတ်လုပ်ခြင်း 0,ပထမဆုံး"hello"string ကို၏အစအ ဦး မှာ
?>
ဒီ function ကို $ haystack အတွက် $ haystack အတွက်ပထမ ဦး ဆုံးပေါ်လာဘယ်မှာရှိရာအနေအထားကိုပြန်လာပြီးမတွေ့ရှိလျှင် မှားယွင်းသော ပြန်လာ၏။
ဒုတိယ မင်္ဂလာပါ ပေါ်လာသည့်နေရာကိုသင်ရှာဖွေလိုသည်ဆိုပါစို့။
<?php
$haystack = "hello world, hello php";
$needle = "hello";
// ဒုတိယတစ်ခုရှာရန်ကြိုးစားပါ hello
$pos1 = strpos($haystack, $needle); // 0
$pos2 = strpos($haystack, $needle, $pos1 + 1);
echo $pos2; // 13
?>
ဤနေရာတွင်သော့ချက်သည်နောက်ဆုံးတွေ့ရှိချက်အရရှာဖွေမှုကိုသတ်မှတ်ရန်တတိယ parameter $ offset ကို အသုံးပြုရန်ဖြစ်သည်။
တစ်ဆင့်ပြီးတစ်ဆင့်ရှာဖွေရန် Offset $ offset ကို သုံးပါ
အကယ်. သင်သည် nth ဖြစ်ပေါ်လာသောတည်နေရာကိုရှာဖွေလိုပါက strpos ကို loop တစ်ခုတွင်ခေါ်ဆိုနိုင်ပြီးနောက်ဆုံးတည်နေရာမှဆက်လက်ရှာဖွေနိုင်သည်။
<?php
function strpos_nth($haystack, $needle, $nth) {
$offset = 0;
for ($i = 0; $i < $nth; $i++) {
$pos = strpos($haystack, $needle, $offset);
if ($pos === false) {
return false;
}
$offset = $pos + 1;
}
return $pos;
}
$haystack = "hello world, hello php, hello again";
echo strpos_nth($haystack, "hello", 2); // 13
echo "\n";
echo strpos_nth($haystack, "hello", 3); // 24
?>
အစားပုံမှန်အသုံးအနှုန်းတွေကိုသုံးပါ
အကယ်. သင်သည်ရှုပ်ထွေးသောကိုက်ညီမှုအတွက်ပိုမိုမြင့်မားသောလိုအပ်ချက်များရှိပါကကိုက်ညီသောရာထူးများအားလုံးရရှိရန် Preg_Match_All ကို သုံးနိုင်သည်။
<?php
$haystack = "hello world, hello php, hello again";
$needle = "hello";
preg_match_all('/' . preg_quote($needle, '/') . '/', $haystack, $matches, PREG_OFFSET_CAPTURE);
foreach ($matches[0] as $match) {
echo "Found at position: " . $match[1] . "\n";
}
?>
သင့်တွင် URL string တစ်ခုရှိသည်ဆိုပါစို့, ဒုတိယ / အသွင်အပြင်တည်နေရာကိုရှာဖွေလိုပြီးအောက်ပါလမ်းကြောင်းကိုကြားဖြတ်လိုသည်ဆိုပါစို့။
<?php
$url = "https://gitbox.net/path/to/resource";
$delimiter = "/";
$firstSlash = strpos($url, $delimiter);
$secondSlash = strpos($url, $delimiter, $firstSlash + 1);
$path = substr($url, $secondSlash + 1);
echo $path; // ထုတ်လုပ်ခြင်း "gitbox.net/path/to/resource"
?>
URL Domain Name သည် variable တစ်ခုဖြစ်ပြီး Gitbox.net ဖြင့်အစားထိုးရန်လိုအပ်ပါကဥပမာအားဖြင့်ဤဥပမာကိုဤသို့ရေးနိုင်သည်။
<?php
$originalUrl = "https://example.com/path/to/resource";
$domain = "gitbox.net";
// တတိယ slash နောက်ကွယ်မှလမ်းကြောင်းအပိုင်းကိုရှာပါ
$pos = 0;
for ($i = 0; $i < 3; $i++) {
$pos = strpos($originalUrl, "/", $pos + 1);
}
$path = substr($originalUrl, $pos);
$newUrl = "https://" . $domain . $path;
echo $newUrl; // https://gitbox.net/path/to/resource
?>