阅读量:5
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 中第一个字符的位置,从而忽略位置。
以上就是关于“php stripos如何忽略位置”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm