- 查找字符串中最后一次出現指定子字符串的位置
可以使用strripos函數來查找字符串中最后一次出現指定子字符串的位置。例如:
$str = "hello world";
$pos = strripos($str, "o");
echo $pos; // 輸出7
- 忽略大小寫進行查找
strripos函數默認是區分大小寫的,如果想要忽略大小寫進行查找,可以使用strcasecmp函數來忽略大小寫進行查找。例如:
$str = "Hello World";
$pos = strripos(strtolower($str), "o");
echo $pos; // 輸出7
- 檢查子字符串是否存在
可以利用strripos函數來檢查子字符串是否存在于字符串中。如果返回的位置大于等于0,則表示子字符串存在,否則不存在。例如:
$str = "hello world";
$pos = strripos($str, "o");
if ($pos >= 0) {
echo "子字符串存在";
} else {
echo "子字符串不存在";
}
- 搜索多個子字符串
可以通過在循環中使用strripos函數來搜索多個子字符串。例如:
$str = "hello world";
$keywords = array("hello", "world");
foreach ($keywords as $keyword) {
$pos = strripos($str, $keyword);
if ($pos >= 0) {
echo "$keyword 存在";
} else {
echo "$keyword 不存在";
}
}
- 搜索多個子字符串的位置
如果需要找到多個子字符串在字符串中出現的位置,可以使用一個數組來存儲這些位置。例如:
$str = "hello world";
$keywords = array("hello", "world");
$positions = [];
foreach ($keywords as $keyword) {
$pos = strripos($str, $keyword);
if ($pos >= 0) {
$positions[] = $pos;
}
}
print_r($positions);