PHP remove all characters before specific string

PhpString

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

You can use strstr to do this.

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

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser547794View Question on Stackoverflow
Solution 1 - PhpxdazzView Answer on Stackoverflow
Solution 2 - PhpWazyView Answer on Stackoverflow
Solution 3 - Phpsaeed arab sheybaniView Answer on Stackoverflow
Solution 4 - PhpthedayturnsView Answer on Stackoverflow