所写函数有两个方面:
false
作为
substr
. 我没有试过,但我很肯定这会导致运行时错误。
在
remove_before
strpos
速度更快,内存占用更少
strstr
.
因此:
function remove_before($needle, $haystack){
$pos = strpos($haystack, $needle);
// No needle found
if (false === $pos)
return $haystack;
return substr($haystack, $pos + strlen($needle));
}
remove_after
:
function remove_after($needle, $haystack){
$pos = strrpos($haystack, $needle);
// No needle found
if (false === $pos)
return $haystack;
return substr($haystack, 0, $pos);
}