Sometimes we need to delete or replace all the unwanted characters from the string. In this post, we will learn how to Remove Special Character From String in PHP. We will use very simple and easy preg_replace() and str_replace() functions in PHP.
In a project when we work we need to sanitize the data sometimes as per requirement. Let’s suppose we have to get the data using API and in that API response, we are getting some special character with strings. And on our website, we do not want to show the data with special characters. In that condition, we need to remove those special characters from the output string in order to fulfill our requirements in the project. As I mentioned PHP provides us two simple ways to do the same. One is using preg_match() and second is using str_replace() function. So let’s understand both and how we can use them to clean or remove the special characters from the string.
Let’s check the below example with preg_replace()
Example 1 : Using preg_replace
<?php function removeSpecialChar($string){ $result = preg_replace('/[^a-zA-Z0-9_ -]/s','', $string); return $result; } echo removeSpecialChar("Text - after ! special \\ /characters # % remove % "); ?>
Ouput:
Text - after special characters remove
The special characters from a string can be easily removed by preg_replace() function in PHP. We have passed the 3 variables to preg_replace function. pattern, replace value, an input string value. The preg_replace() function performs a search with the regular expression and replaces the matches with a specified replacement.
Example 2 : Using str_replace
<?php function removeSpecialChar($string){ $result = str_replace( array( '\'', '"', '#' , ';', '%', '>', '!' ), '', $string); return $result; } echo removeSpecialChar("Text - after ! special characters # % remove % "); ?>
Output
Text - after special characters remove
If you feel preg_replace() function a little bit confusing then you can use str_replace() which is very easy to use.
You need to pass the 3 parameters in the function first would be the string which you want to replace, it could be a single value or array, then the second value would be what you want to replace the special character with if you want to put another character value in place of special character you can do so, or if you want to remove that special character then just pass inverted commas without space and the third would be a string in which we are going the make changes.
It is very easy and good if you wan to remove or replace specific character from the string.
Clean String for SEO Friendly URL
With the help of following code you can clean the string and use that in making SEO friendly URL.
function cleanStr($string){ // Replaces all spaces with hyphens. $string = str_replace(' ', '-', $string); // Removes special chars. $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Replaces multiple hyphens with single one. $string = preg_replace('/-+/', '-', $string); return $string; } $cleanStr = cleanStr($string);
Conclusion:
We have tried to explain the topic with the help of two examples, you can use anyone in your project. According to me, If you need to replace all the special characters from the string then use pre_repalce() or if you want to replace just only a few characters or one character from the string then use str_replace(). You can remove the special character using javascript too, Please check our post to How to Remove HTML Tags from String using JavaScript