Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-在上次发生后删除所有内容,并可选择删除指针_Php_String_Function - Fatal编程技术网

PHP-在上次发生后删除所有内容,并可选择删除指针

PHP-在上次发生后删除所有内容,并可选择删除指针,php,string,function,Php,String,Function,我正在尝试创建一个函数,该函数将删除从单词最后一次出现到字符串结尾的所有内容,并具有删除该单词的选项 例如,如果我在“奶酪是好的”和“奶酪是美味的”中搜索奶酪,则“奶酪是美味的”将被删除 但是我也想要一个选项来删除我搜索的单词。如果是那样的话,我最后会吃到奶酪很好而且味道很好 我有一个很好的函数,它从一个带有选项的字符串的开头开始执行此操作。看起来像这样。。。但我似乎不能做相反的事情 function remove_before($needle, $haystack, $removeNeedle

我正在尝试创建一个函数,该函数将删除从单词最后一次出现到字符串结尾的所有内容,并具有删除该单词的选项

例如,如果我在“奶酪是好的”和“奶酪是美味的”中搜索奶酪,则“奶酪是美味的”将被删除

但是我也想要一个选项来删除我搜索的单词。如果是那样的话,我最后会吃到奶酪很好而且味道很好

我有一个很好的函数,它从一个带有选项的字符串的开头开始执行此操作。看起来像这样。。。但我似乎不能做相反的事情

function remove_before($needle, $haystack, $removeNeedle=false) {
    $pos = strpos($haystack, $needle);
    if ($pos!==false)
        return substr($haystack, $pos + (strlen($needle) * $removeNeedle) );
    else
        return $haystack;
}

我认为这解决了问题:

function remove_before($needle, $haystack, $removeNeedle=false) {
    $pos = strrpos($haystack, $needle);
    if ( !($pos === false) ) {
        if( !$removeNeedle )
            $pos += strlen($needle);
        $haystack = substr($haystack, 0, $pos);
    }
    return $haystack;
}

var_dump(remove_before("cheese", "The cheese is good and the cheese is yummy", true));
// Outputs: "The cheese is good and the "

var_dump(remove_before("cheese", "The cheese is good and the cheese is yummy"));
// Outputs: "The cheese is good and the cheese"

我认为这解决了问题:

function remove_before($needle, $haystack, $removeNeedle=false) {
    $pos = strrpos($haystack, $needle);
    if ( !($pos === false) ) {
        if( !$removeNeedle )
            $pos += strlen($needle);
        $haystack = substr($haystack, 0, $pos);
    }
    return $haystack;
}

var_dump(remove_before("cheese", "The cheese is good and the cheese is yummy", true));
// Outputs: "The cheese is good and the "

var_dump(remove_before("cheese", "The cheese is good and the cheese is yummy"));
// Outputs: "The cheese is good and the cheese"
使用以下remove_after功能:

function remove_after($needle, $haystack, $removeNeedle = false) {
    // getting the position of the last occurence of `$needle`
    $last_pos = strrpos($haystack, $needle);
    // if the `$needle` is found within `$haystack`
    if (($last_pos = strrpos($haystack, $needle)) !== false) {
        return substr($haystack, 0, $removeNeedle? $last_pos : $last_pos + strlen($needle));
    }
    return $haystack;
}

var_dump(remove_after("cheese", "The cheese is good and the cheese is yummy"));
var_dump(remove_after("cheese", "The cheese is good and the cheese is yummy", true)); 
输出:

string(33) "The cheese is good and the cheese"
string(27) "The cheese is good and the "
使用以下remove_after功能:

function remove_after($needle, $haystack, $removeNeedle = false) {
    // getting the position of the last occurence of `$needle`
    $last_pos = strrpos($haystack, $needle);
    // if the `$needle` is found within `$haystack`
    if (($last_pos = strrpos($haystack, $needle)) !== false) {
        return substr($haystack, 0, $removeNeedle? $last_pos : $last_pos + strlen($needle));
    }
    return $haystack;
}

var_dump(remove_after("cheese", "The cheese is good and the cheese is yummy"));
var_dump(remove_after("cheese", "The cheese is good and the cheese is yummy", true)); 
输出:

string(33) "The cheese is good and the cheese"
string(27) "The cheese is good and the "

我明白了我最初想如何解决这个问题。类似于我的remove_before函数。。。。因为keepNeedle将是1或0,所以可以在子字符串中简单地将其相乘,这样就不需要多个If-Else语句

function remove_after($needle, $haystack, $keepNeedle=true) {
$last_pos = strrpos($haystack, $needle);
if ($last_pos!== false) return substr($haystack,0,$last_pos+(strlen($needle)*$keepNeedle));
return $haystack;}

我明白了我最初想如何解决这个问题。类似于我的remove_before函数。。。。因为keepNeedle将是1或0,所以可以在子字符串中简单地将其相乘,这样就不需要多个If-Else语句

function remove_after($needle, $haystack, $keepNeedle=true) {
$last_pos = strrpos($haystack, $needle);
if ($last_pos!== false) return substr($haystack,0,$last_pos+(strlen($needle)*$keepNeedle));
return $haystack;}

preg\u replace'.*\bcheese\b.*~s','$1',$s或preg\u replace'.*\bcheese\b.*~s','$1',$s.preg\u replace'.*\bcheese\b.*~s','$1',$s或preg\u replace'.*\bcheese\b.*~s','1',$s。谢谢,请帮助我理解。。。。这是什么意思?还有:你在地铁站吗?这是不是像一个古老的学校如果其他声明?有没有办法让它更具可读性?谢谢你的帮助。我现在明白了,这是一种更简单的写作方式。。。如果$removeNeedle==真实返回substr$haystack,0,$last_pos;否则返回substr$haystack,0,$last_pos+strlen$needle@奶酪味的,是的,它叫做三元运算符。谢谢,帮我理解。。。。这是什么意思?还有:你在地铁站吗?这是不是像一个古老的学校如果其他声明?有没有办法让它更具可读性?谢谢你的帮助。我现在明白了,这是一种更简单的写作方式。。。如果$removeNeedle==真实返回substr$haystack,0,$last_pos;否则返回substr$haystack,0,$last_pos+strlen$needle@奶酪味的,是的,它叫做三元运算符。