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

  function RemoveSpecialChar($str) {

      $res = str_replace( array( '\'', '"',

      ',' , ';', '<', '>' ), ' ', $str);

      return $res;

      }

  $str = "Example,to removeSpecial'Char;";

  $str1 = RemoveSpecialChar($str);

  echo $str1;

?>

Output:

Example to remove the Special Char

Using str_ireplace() Method: The str_ireplace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “). The difference between str_replace and str_ireplace is that str_ireplace is case-insensitive.

Syntax:

str_ireplace( $searchVal, $replaceVal, $subjectVal, $count )

Example: This example illustrates the use of the str_ireplace() method to remove all the special characters from the string.

PHP

  function RemoveSpecialChar($str){

      $res = str_ireplace( array( '\'', '"',

      ',' , ';', '<', '>' ), ' ', $str);

      return $res;

      }

  $str = "Example,to removeSpecial'Char;";

  $str1 = RemoveSpecialChar($str);

  echo $str1;

?>

Output:

Example to remove the Special Char

Using preg_replace() Method: The preg_replace() method is used to perform a regular expression for search and replace the content.

Syntax:

preg_replace( $pattern, $replacement, $subject, $limit, $count )

Example: This example illustrates the use of the preg_replace() method where the particular or all the matched pattern found in the input string will be replaced with the substring or white space.

PHP

  function RemoveSpecialChar($str){

      $res = preg_replace('/[^a-zA-Z0-9_ -]/s',' ',$str);

      return $res;

  }

  $str = "Example,to removeSpecial'Char;";

  $str1 = RemoveSpecialChar($str);

  echo $str1;

?>

Output:

Example to remove the Special Char

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


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 can I remove all characters from 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.

How do you remove portion of a string before a certain 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 trim a string after a specific character in PHP?

The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string. rtrim() - Removes whitespace or other predefined characters from the right side of a string.