How to Remove Special Character from String in PHP

In this tutorial I will discuss how to remove special character from string in PHP. Sometimes we need to get result of an input string as a simple composition of alphabets and numbers and we want to remove all special characters by using preg_replace. There are two option available to remove special character from string using php.

You can also check other tutorial of string,

Option 1: Using preg_replace PHP Function

We can remove special character from sting using preg_replace,as like name its using regular expression to special character from string.

Syntax of preg_replace PHP Function:

$result = preg_replace($pattern, $replaceWith , $string);
$result = This is the output string without special characters.
$string = This is the string which you want filter special characters.

Remove Special Character from String in PHP:

How to Call Method:

echo $obj->RemoveSpecialChar("does't happened ' ' test");

Result:

Option 1: Using str_replace PHP Function

You can also use another PHP function str_replace() to get the same result as above script, if we know what all we have to remove special characters.

Syntax of str_replace PHP Function:

$title = str_replace( array( '\'', '"', ',' , ';', '<', '>' ), ' ', $value);

Example to Remove Special Character from String in PHP:

How to call Method

echo $obj->RemoveSpecialChar("does't happened ' ' test");

Result: