How do i remove all characters before a specific 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,72010 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,7364 gold badges32 silver badges38 bronze badges

You are here

Home » Tools & Tips » PHP: Remove Everything Before and Including a Character/String

This can be achieved using string manipulation functions in PHP. First we find the position of the desired character in the string using strpos[], substr[], ltrim, and trim[]

$new_name = substr[$old_name, strpos[$old_name, '_'] + 1];

or

$new_name = ltrim[stristr[$old_name, '_'], '_'];

In this article, we will see how to remove special characters from strings in PHP, along with understanding their implementation through the examples. We have given a string and we need to remove special characters from string str in PHP, for this, we have the following methods in PHP:

Using str_replace[] Method: The str_replace[] methodis used to remove all the special characters from the given string str by replacing these characters with the white space [” “].

Syntax:

str_replace[ $searchVal, $replaceVal, $subjectVal, $count ]

Example: This example illustrates the use of the str_replace[] function to remove the special characters from the string.

PHP

Chủ Đề