98

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.

Charles
  • 50,943
  • 13
  • 104
  • 142
user547794
  • 14,263
  • 36
  • 103
  • 152
  • You might find [`s($str)->afterFirst('www/audio')`](https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str.php#L435) or [`s($str)->afterLast('www/audio')`](https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str.php#L445) helpful, as found in [this standalone library](https://github.com/delight-im/PHP-Str). – caw Jul 27 '16 at 00:33
  • For the single character, check: [How to remove everything before the first specific character in a string?](https://stackoverflow.com/q/5329866/55075) – kenorb Nov 21 '18 at 13:26

4 Answers4

195

You can use strstr to do this.

echo strstr($str, 'www/audio');
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • 40
    Quick note: you can use `strstr()` to return the part of the string that comes **before** its argument too by calling `echo strstr($str, 'www/audio', true);` – cfx Aug 09 '13 at 11:30
23

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];
Wazy
  • 8,822
  • 10
  • 53
  • 98
  • string functions are generally faster than array functions – Anyone Jan 18 '14 at 11:20
  • 4
    The explode is in fact a better answer, as the question was about _removing_ the text before the string. – Borjovsky Feb 29 '16 at 14:13
  • 1
    explode could cause problems if you had multiple occurrences of the same string you were trying to remove - e.g. "we have www/audio path where the www/audio files are stored" would result in "path where the" if you wanted to use explode you could do something like foreach ( $expStr as $key => $value ) echo ( $key == 0 ? '' : 'www/audio'.$value ) ; or foreach ( $expStr as $key => $value ) echo ( $key == 0 ? '' : ( $key == 1 ? '' : 'www/audio' ).$value ) ; if you wanted to omit the initial www/audio. – TheKLF99 Mar 18 '19 at 07:47
2

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);
        }
    }
}
Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
0

You can use substring and strpos to accomplish this goal.

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.

thedayturns
  • 9,723
  • 5
  • 33
  • 41