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

时间:2022-12-28 20:22:21

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

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

"www/audio"

Not sure how I can do this.

我不知道该怎么做。

5 个解决方案

#1


130  

You can use strstr to do this.

您可以使用strstr进行此操作。

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

#2


14  

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];

#3


0  

You can use substring and strpos to accomplish this goal.

您可以使用子字符串和strpos来实现这个目标。

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

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

#4


0  

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);
        }
    }
}

#5


-5  

Replay on the functions

回放的功能

$str = 'https://pbs.twimg.com/media/Ce-IZnCW4AEtiG4.jpg';

美元str = ' https://pbs.twimg.com/media/Ce-IZnCW4AEtiG4.jpg ';

echo strright($str, '/');

回声strright(str美元,' / ');

returns 4.jpg

返回4. jpg

#1


130  

You can use strstr to do this.

您可以使用strstr进行此操作。

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

#2


14  

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];

#3


0  

You can use substring and strpos to accomplish this goal.

您可以使用子字符串和strpos来实现这个目标。

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

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

#4


0  

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);
        }
    }
}

#5


-5  

Replay on the functions

回放的功能

$str = 'https://pbs.twimg.com/media/Ce-IZnCW4AEtiG4.jpg';

美元str = ' https://pbs.twimg.com/media/Ce-IZnCW4AEtiG4.jpg ';

echo strright($str, '/');

回声strright(str美元,' / ');

returns 4.jpg

返回4. jpg