How do you remove portion of a string before a certain character in php?

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.3k13 gold badges101 silver badges141 bronze badges

asked Oct 18, 2011 at 5:33

user547794user547794

13.8k35 gold badges100 silver badges150 bronze badges

2

You can use strstr to do this.

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

answered Oct 18, 2011 at 5:36

1

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

answered Oct 18, 2011 at 5:39

WazyWazy

8,71010 gold badges54 silver badges97 bronze badges

3

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

answered Oct 18, 2011 at 5:45

2

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.

answered Oct 18, 2011 at 5:37

thedayturnsthedayturns

8,7164 gold badges32 silver badges38 bronze badges

Not the answer you're looking for? Browse other questions tagged php string or ask your own question.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The substr() and strpos() function is used to remove portion of string after certain character.
    strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string. Function treats upper-case and lower-case characters differently.

    Syntax:

    strpos( original_string, search_string, start_pos )

    Return Value: This function returns an integer value which represents the index of original_str where the string search_str first occurs.

    substr() function: The substr() function is an inbuilt function in PHP is used to extract a specific part of string.

    Syntax:

    substr( string_name, start_position, string_length_to_cut )

    Return Value: Returns the extracted part of the string if successful otherwise FALSE or an empty string on failure.
    Here are few examples.

    Below examples use substr() and strpos() function to remove portion of string after certain character.

    Example 1:

    <?php

    $variable = "GeeksForGeeks is the best platform.";

    echo $variable;

    echo "\n";

    $variable = substr($variable, 0, strpos($variable, "is"));

    echo $variable;

    ?>

    Output:

    GeeksForGeeks is the best platform.
    GeeksForGeeks
    

    Example 2:

    <?php

    $variable = "computer science portal for geeks";

    echo $variable;

    echo "\n";

    echo substr($variable, 0, strpos($variable, "for"));

    ?>

    Output:

    computer science portal for geeks
    computer science portal
    


    How can I remove part of a string after a specific character in PHP?

    The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.

    How do I remove all characters from a string before a specific character?

    To remove everything before the first occurrence of the character '-' in a string, pass the character '-' as a separator in the partition() function. Then assign the part after the separator to the original string variable. It will give an effect that we have deleted everything before the character '-' in a string.

    How do I remove all characters before a specific character in PHP?

    You can use strstr to do this. Show activity on this post. The explode is in fact a better answer, as the question was about removing the text before the string.

    How do I remove all characters from a string after a specific character?

    Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.