stristr 是 PHP 中的一个字符串搜索函数,它从给定的字符串中查找首次出现的子字符串
function stristr($haystack, $needle) {
if ($needle === '') {
return $haystack;
}
$pos = strpos($haystack, $needle);
if ($pos === false) {
return '';
} else {
return substr($haystack, $pos);
}
}
在这个函数中,我们首先检查 $needle 是否为空字符串。如果是空字符串,我们直接返回整个 $haystack,因为从空字符串中找不到任何子字符串。
接下来,我们使用 strpos 函数查找 $haystack 中首次出现的 $needle 的位置。如果找到了(即 $pos 不为 false),我们使用 substr 函数从 $haystack 中提取子字符串,从 $pos 开始到原字符串末尾。如果没有找到(即 $pos 为 false),我们返回一个空字符串。
这样,我们可以处理 stristr 函数可能产生的错误情况,例如当 $needle 为空字符串时,或者当 $haystack 中不存在 $needle 时。

便宜VPS测评









