stripos 是 PHP 中的一个字符串函数,用于查找子字符串在另一个字符串中首次出现的位置,不区分大小写。然而,stripos 本身并不提供直接的参数来忽略子字符串的位置。要实现这个功能,你可以使用以下方法:
- 使用
strpos函数代替stripos,然后使用str_word_count函数来获取子字符串在字符串中的位置。最后,使用array_search函数找到子字符串在所有单词中的位置,从而忽略位置。
示例代码:
function get_position_ignore_position($haystack, $needle) {
$haystack_words = str_word_count($haystack, 1);
$needle_position = array_search($needle, $haystack_words);
if ($needle_position !== false) {
// Subtract the position of the first character in the haystack
$position = $needle_position - 1;
} else {
$position = -1;
}
return $position;
}
$haystack = "This is a test string";
$needle = "is";
$position = get_position_ignore_position($haystack, $needle);
echo "Position: " . $position; // Output: Position: 2
这个示例中,我们定义了一个名为 get_position_ignore_position 的函数,该函数接受两个参数:$haystack 和 $needle。我们首先使用 str_word_count 函数将 $haystack 分割成单词数组,然后使用 array_search 函数找到 $needle 在数组中的位置。最后,我们从位置中减去 $haystack 中第一个字符的位置,从而忽略位置。

便宜VPS测评









