php

How to Remove Special Characters from String In PHP

Sometimes, you may need to remove special characters from a string for programming purposes. PHP has many built-in functions that can be used to remove special characters from string data. This tutorial shows you how to use a few different types of PHP built-in functions to remove special characters from a string.

The str_replace() Function

One useful function that can be used to remove special characters from a string is the str_replace() function. The empty string must be used to the replace character in this function to remove the specified character. The syntax of this function is given below.

 str_replace($search_str, $replace_str, $main_str [,$count ])

The str_replace() function can take four arguments. The first three arguments are mandatory, and the last argument is optional. The $search_str variable stores the value that will be searched in the string. The $replace_str variable stores the value that will be replaced from the string where the search text matches. You must use an empty string as the value of the $replace_str variable to remove the search text from the main string. The $count argument stores a value representing how many characters are replaced or removed.

Example: Using str_replace() to Remove Special Characters

The following script shows the use of the str_replace() function to remove a special character from a string of data. A user-defined function is declared in the script to perform the replace task. The hash(#), single quote(‘), and semicolon(;) characters are used as search characters, while the empty string is used as the replacement text for these characters.

 <?php

/* The following script will remove some

special characters from a string using <strong>str_replace()</strong>

function

*/


 

//Define the main string

$mainstr = "#This is a sim'ple text;";

 

//The output before remove

echo "<b>Text before remove: </b> <br/>".$mainstr;

 

//Call the function

$replacestr = rm_special_char($mainstr);

 

//Define the function to remove the spacial character

function rm_special_char($str) {

//Remove "#","'" and ";" using str_replace() function

$result = str_replace( array("#", "'", ";"), '', $str);

//The output after remove

echo "<br/><b>Text after remove: </b> <br/>".$result;

}

?>

Output

The following output will appear after running the script given above. The value of the main text is printed before calling the str_replace() function, and the three special characters are removed from the main text and printed later.

The preg_replace() Function

The preg_replace() function is used to replace string data based on the searching pattern. The syntax of this function is given below.

 preg_replace( $pattern, $replace, $string [,$limit [,$count]] )

This function can take five arguments. The first three arguments are mandatory, and the last two arguments are optional. The $pattern argument is used to define the pattern to search the character(s) in a string. The $replace argument is used to define the replacement text, and the replacement text will be an empty string used for removing the special characters. The $string argument is used to define the main string in which the pattern will be searched and replaced.

Example: Using  preg_replace() to Remove Special Characters

The following script shows the use of the preg_replace() function to remove a particular special character from the string data. The ‘[0-9/[0-9%$?]/s’ pattern is used in the function to search for the characters. This will search all characters ‘%,’ ‘$,’ and ‘?’ in the string data and replace these characters with the empty string if the characters exist.

 <?php

/* The following script will remove some

special characters from a string using preg_replace()

function

*/


 

//Define the main string

$mainstr = "200I like $php programming50%?.";

 

//The output before remove

echo "<b>Text before remove: </b> <br/>".$mainstr;

 

//Call the function

$replacestr = rm_special_char($mainstr);

 

//Define the function to remove the spacial character

function rm_special_char($str) {

//Remove "#","'" and ";" using str_replace() function

$result = preg_replace('/[0-9%$?]/s','', $str);

//The output after remove

echo "<br/><b>Text after remove: </b> <br/>".$result;

}

?>

Output

The following output will appear after running the script. The value of the main text will be printed before calling the preg_replace() function. The 200, 50, ‘%,’ and ‘?’ characters will be removed from the main text and printed later.

The htmlspecialchars() and str_ireplace() Functions

The htmlspecialchars() and str_ireplace() functions are used to convert all predefined characters into HTML. For example, ‘<’ will be converted into ‘&lt,’ ‘&’ will be converted into ‘&amp,’ etc. You can use these functions to remove the effect of any predefined characters from the string data.

Example: Using htmlspecialchars() and str_ireplace() to Remove Special Characters

The following script shows how to remove the effect of predefined characters from a string by using the htmlspecialchars() function. Then, the str_ireplace() function is used to remove the HTML entities from the text. The str_ireplace() function works like the str_replace() function, but it can perform case-insensitive searches. The main string contains text with <h2> and <b> tags. So, when the text prints before removing the effect of the predefined characters, the string will be displayed with the effect of HTML header and bold tags. The plain text will be displayed after applying the given functions.

 <?php

 

//Define the main string

$mainstr = "<h2>Welcome to <b>LinuxHint</b></h2>";

 

//The output with HTML tags

echo "Text before remove: ".$mainstr;

 

//The output after removing HTML tags

echo "Text after remove: <br/>".

str_ireplace(array('&lt;b&gt;', '&lt;/b&gt;','&lt;h2&gt;','&lt;/h2&gt;'),'',

htmlspecialchars($mainstr));

 

?>

Output

The following output will appear after running the script.

The trim() Function

The trim() function is used to remove specific characters from the starting and ending of a string of data. This function cannot remove characters from the middle of a string of data. So, you can use this function only if you want to remove special characters from the start and end of a string.

Example: Using the trim() Function to Remove Special Characters

The following script shows the use of the trim() function to remove the ‘@’ and ‘!’ characters from the start and end of the string variable $mainstr. The value of the $mainstr variable will be printed both before and after removing the characters.

 <?php

 

//Define the main string

$mainstr = "@@Web Programming!!!.";

 

//The output before using trim()

echo "<b>Text before remove: </b> <br/>".$mainstr;

 

//The output after using trim()

echo "<br/><b>Text after remove: </b> <br/>".trim($mainstr,'@!');

 

?>

Output

The following output will appear after running the script. Here, two ‘@’ characters are removed from the beginning, and three ‘!’ characters are removed from the end of the string variable $mainstr.

Conclusion

This tutorial showed you four different ways of removing special characters from string data. I hope that this tutorial will help readers to apply the functions provided in this article in their script.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.