Php 在()之前删除_

Php 在()之前删除_,php,string,Php,String,有没有更好的方法来完成下面的工作? (不存在致命错误的可能性) 与strstrstr($haystack,$needle)类似,但返回的字符串中没有针, 我不妨问一下,这是否也可以改进 function remove_after($needle,$haystack){ return substr($haystack, 0, strrpos($haystack, $needle)); } 请注意,在最后一次出现打捆针之前,在剥线后移除线,在第一次出现打捆针之前,在剥线前移除线 编辑: 例

有没有更好的方法来完成下面的工作? (不存在致命错误的可能性)

与strstrstr($haystack,$needle)类似,但返回的字符串中没有针, 我不妨问一下,这是否也可以改进

function remove_after($needle,$haystack){
    return substr($haystack, 0, strrpos($haystack, $needle));
}
请注意,在最后一次出现打捆针之前,在剥线后移除线,在第一次出现打捆针之前,在剥线前移除线

编辑: 例如:

编辑:
我将把它留在这里供其他人参考。

所写函数有两个方面:

它们没有错误处理。例如,在remove_before:needle not in haystack中,使其通过
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));
}
同样地,
之后删除\u:

function remove_after($needle, $haystack){
    $pos = strrpos($haystack, $needle);
    // No needle found
    if (false === $pos)
        return $haystack;
    return substr($haystack, 0, $pos);
}

有什么原因需要更好的方法吗?为什么不使用strstr函数?@Col.Shrapnel,因为我认为php有一个内置函数来实现这个功能@科德勒,因为它不一样??还是这样?我试过了,它给了我一个不同的结果。更不用说你可以自己检查手册中的功能列表,但即使有这样的功能-那又怎样?你自己的有什么不好?我也确信它会返回运行时错误,但它没有。IMO PHP需要内置这些函数。。。
function remove_before($needle, $haystack){
    $pos = strpos($haystack, $needle);
    // No needle found
    if (false === $pos)
        return $haystack;
    return substr($haystack, $pos + strlen($needle));
}
function remove_after($needle, $haystack){
    $pos = strrpos($haystack, $needle);
    // No needle found
    if (false === $pos)
        return $haystack;
    return substr($haystack, 0, $pos);
}