Remove Special Character in PHP Using str_replace()

Today, I will discuss how to remove special characters from string in php using str_replace(), In order to do this task, PHP have str_replace() function to remove special characters from string str in PHP.

Normally programmers are using regular expressions to remove special characters from string with the help of preg_replace() method.

You can also check other tutorial of PHP string:

Let’s create PHP method to remove special characters from strings.

Simple Example to Escape special characters in PHP

We’ll use str_replace method to remove special characters from a string, I have created a config.php file that will have escapeSequenceValidation variable to store all special characters, that need to remove from the source string.

Using str_replace PHP Function

The str_replace() is a built-in function in PHP and is used to replace all the occurrences of the search string. This function returns a string or an array.

Syntax

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

The Parameters are:

  • searchVal(Required) : This parameter specifies the string to be searched and replaced. It can be either a string or an array with strings.
  • replaceVal(Required) : This parameter specifies the string with which we want to replace the searchVal string. It can be either a string or an array with strings.
  • subjectVal(Required) : This parameter specifies the string or array of strings which we want to search for searchVal and replace with replaceVal. It can be either a string or an array with strings.
  • $count(Optional) : This parameter is optional and if passed, its value will be set to the total number of replacement operations performed on the string subjectVal.
//target string
$value ="Hi, I am ~ parvez $ alam";
    
echo 'before = ' .$value;

//get escape string from config file separated with ':'
//ex -  ~:$
echo $escapeChars = $config['escapeSequenceValidation'];
//if exist
if($escapeChars) {
//explode string with delimiter
$arrEscapeChars =  explode(':', $escapeChars);
print_r($arrEscapeChars);
//replace escape characters array values with null values
$value = str_replace($arrEscapeChars, '', $value);
}
echo 'after = '.$value;

Output:

before = Hi, I am ~ parvez $ alam
~:$
Array
(
    [0] => ~
    [1] => $

)

after = Hi, I am parvez $ alam

Remove Special Characters Using Regular Expression

We can also use preg_replace() method remove special characters from string in php. It’s used to perform a regular expression search and replace. This function searches for pattern in subject parameter and replaces them with the replacement.

The preg_replace() method is a built-in function of PHP that also remove special character from string. You can use this method with one array or array of patterns with a replacement string.

preg_replace(patterns, replacements, input, limit, count)

The Parameters are:

  • patterns(Required) : Contains a regular expression or array of regular expressions
  • replacements(Required) : A replacement string or an array of replacement strings
  • input(Required) : The string or array of strings in which replacements are being performed
  • limit(Optional) : The defaults is -1, means unlimited. Sets a limit to how many replacements can be done in each string.
  • count(Optional) : After the function has executed, this variable will contain a number indicating how many replacements were performed.
$str = "Hi, I am ~ parvez $ alam";
function clean($label) {
return preg_replace('/[^A-Za-z0-9-]/', ' ', $label); // Removes special chars.
}
echo clean($str);

Output is:

Hi I am parvez alam

References:

Leave a Reply

Your email address will not be published. Required fields are marked *