php PHP删除特定字符串之前的所有字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7802821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:23:48  来源:igfitidea点击:

PHP remove all characters before specific string

phpstring

提问by user547794

I need to remove all characters from any string before the occurrence of this inside the string:

我需要在字符串中出现 this 之前从任何字符串中删除所有字符:

"www/audio"

Not sure how I can do this.

不知道我怎么能做到这一点。

回答by xdazz

You can use strstrto do this.

您可以使用strstr来执行此操作。

echo strstr($str, 'www/audio');

回答by Wazy

Considering

考虑

$string="We have www/audio path where the audio files are stored";  //Considering the string like this

Either you can use

你可以使用

strstr($string, 'www/audio');

Or

或者

$expStr=explode("www/audio",$string);
$resultString="www/audio".$expStr[1];

回答by saeed arab sheybani

I use this functions

我使用这个功能

function strright($str, $separator) {
    if (intval($separator)) {
        return substr($str, -$separator);
    } elseif ($separator === 0) {
        return $str;
    } else {
        $strpos = strpos($str, $separator);

        if ($strpos === false) {
            return $str;
        } else {
            return substr($str, -$strpos + 1);
        }
    }
}

function strleft($str, $separator) {
    if (intval($separator)) {
        return substr($str, 0, $separator);
    } elseif ($separator === 0) {
        return $str;
    } else {
        $strpos = strpos($str, $separator);

        if ($strpos === false) {
            return $str;
        } else {
            return substr($str, 0, $strpos);
        }
    }
}

回答by thedayturns

You can use substringand strposto accomplish this goal.

您可以使用substringstrpos来实现此目标。

You could also use a regular expressionto pattern match only what you want. Your mileage may vary on which of these approaches makes more sense.

您还可以使用正则表达式来模式匹配您想要的内容。您的里程可能因这些方法中的哪一种更有意义而有所不同。