Remove Special Characters from String PHP

In this article, you going to see how to remove special characters from string in PHP using simple example with step by step.

If you are looking for this topic, then you are at right place.

Many time user in need of string which consist of number and alphabet.

User needs to remove the special characters from string, at that time you can use method to achieve this.

Let’s start with the topic of Remove Special Characters from String PHP

There are two methods with the help of which we can remove special characters from the string.

Method 1:Function called PHP preg_replace() Function to remove special characters.

It returns a string or array of strings by matching with the pattern passing as a parameter or you can say regular expression.

From the below syntax you can understand regarding PHP preg_replace() Function.

preg_replace(patterns, replacements, input, limit, count)
ParameterDescription
patternsIt is Required. It is consist of regular expression or array of regular expressions
replacementsIt is Required. It consists of a replacement string or an array of replacement
inputIt is Required. It is a string or array in which replacements are going to be performed
limitIt is Optional. It is set, Defaults to -1, meaning unlimited. Sets a limit to how many replacements.
countIt is Optional.
Parameter Value Description

Below is the code snippet from which you can understand easily, it consist of a function in which passing a parameter.

<?php
function RemoveSpecialChar($value){
$result  = preg_replace('/[^a-zA-Z0-9_ -]/s','',$value); 
return $result;
}
echo RemoveSpecialChar("It's just a sample ' ' test to remove special character");
//Output : Its just a sample test
?>

Method 2:Function called str_replace PHP Function used to remove special characters

The str_replace() function use to replaces some characters bypassing as parameter with some other characters in a string which is also get passed as next to the parameter.

Below is the syntax from which you can understand easily.

str_replace(find,replace,string,count)
ParameterDescription
findIt is Required. Represents the value which is to be find
replaceIt is Required. Represents the value which is to be replaced the value in find
stringIt is Required. Represents the string which is to be searched
countIt is Optional, which going to counts the number of replacements
Parameter Value String Replace

Below code snippet from which you can understand.

<?php
function RemoveSpecialChar($value){
$variable = str_replace( array( '\'', '"', ',' , ';', '<', '>'), '', $value);
return $variable;
}
echo RemoveSpecialChar("It is just a sample ' ' test to remove special character");
// Output It is just a sample test to remove special character
?>

How to remove html special characters from a string in PHP ?

There are some specific characters, which have have some special importance in HTML they are given below.

 (&, ", ', <, >) 

And this converted into HTML entities.

Then after which you can use PHP htmlspecialchars() function to convert special characters in a string to HTML entities.

 <?php
$var_str = "Sample String with <b>special</b> characters.";
// Removing HTML special characters
echo htmlspecialchars($var_str);
?>
If you going to see view source code of above string
"Sample String with <b>special</b> characters." converted to
Sample String with &lt;b&gt;special&lt;/b&gt; characters.

And browser you will see ouput "Sample String with <b>special</b> characters.";

If you want to replace complete html tag also than you also use preg_replace function which is explained above.

How to remove all special characters from a string in PHP?

In similar way if user in need to remove the all the special character from string in PHP.

User can go for using str_replace and preg_replace PHP function.

From below code snippet you can understand easily.

<?php
function RemoveAllSpecialChar($vars) {
   $var = str_replace(' ', '-', $vars); // Replaces all spaces with hyphens.
   return preg_replace('/[^A-Za-z0-9\-]/', '', $var); // Removes special chars.
}
echo RemoveAllSpecialChar('a|"bc!@£de^&$f g');
//output: abcdef-g
?>

The Ultimate Guide To print_r in PHP

How to remove multiple special characters from a string in PHP?

By using above code snippet it is possible to achieve in removing multiple or you can say the special characters from string.

So please check with the above code snippet.

How to remove spaces and special characters from string in PHP?

To remove space from the string you can use the function called str_replace the function which is completely explained in the above code snippet.

To remove special characters from the string you can use the function called preg_replace function bypassing regular expression as a match pattern which is explained in above code snippet.

Conclusion:

Finally, we have done with discussion regarding remove special characters from string in PHP.

I hope you like this article and if you feel like we missed out on anything, please comment below.

Leave a Comment