Last Updated on December 27, 2020 by Roshan Parihar
In this tutorial, learn how to remove special characters from string using PHP. The short answer is to use the PHP preg_replace()
to remove all special characters or PHP str_replace()
for only specified characters from a string. Convert special characters to HTML entities in PHP.
PHP comes with many useful functions whose examples given below. Find out how you can use these functions to delete those characters from the given string in PHP.
Remove All Special Characters From String Using PHP
If you want to remove all special characters from the string with PHP. You have to use PHP preg_replace()
which replaces or removes all the special characters. It also removes dots(.) or full stop from the string using PHP.
1 2 3 4 5 |
<?php $myString = "This isn't my <strong>bold</strong> string. &I'm working."; $result = preg_replace('/[^a-zA-Z0-9_ -]/s','',$myString); echo $result; ?> |
Output
The above example contains the string with some special characters. The output of the example contains no special characters. It removes the special characters without any space in place of them.
It contains the HTML strong tag and output contains no HTML entities. If you don’t want to remove HTML entities, you need to read the last topic of this tutorial.
Eliminate Specified Special Characters From String in PHP
If you want to remove only the specified special characters from the string. You have to use the PHP str_replace()
function that replaces only the specified special characters.
The function helps to delete single or multiple characters from the string.
Remove Single Specified Character from String
To remove the single character, you have to specify the special character in the first argument and the replacement in the second argument.
1 2 3 4 5 |
<?php $myString = "This isn't my <strong>bold</strong> string. &I'm working."; $strsingleresult = str_replace('&', '', $myString); echo $strsingleresult; ?> |
Output
The above example removes the special character ‘&’ from the string with no space. The string contains the ‘&’ character and the output contains no special character ‘&’.
Remove Multiple Specified Characters from String
In addition to the above example of removing the single character. You can also remove multiple characters from the string.
You have to put all the special characters in the first argument which is an array of special characters.
1 2 3 4 5 |
<?php $myString = "This isn't my <strong>bold</strong> string. &I'm working."; $strresult = str_replace(array('<', '>', '/'), '', $myString); echo $strresult; ?> |
Output
The above example removes only the multiple specified characters from the string. The example contains 3 specified special characters in an array to remove using PHP.
Convert Special Characters to HTML Entities Using PHP
You can also convert the HTML special characters to HTML entities. The function converts 5 special HTML characters &, ', ", <, >
to HTML entities. You have to use htmlspecialchars()
to convert special characters to entities.
1 2 3 4 5 |
<?php $myString = "This isn't my <strong>bold</strong> string. &I'm working."; $htmlstrresult = htmlspecialchars($myString); echo $htmlstrresult; ?> |
Output
The above example contains 3 special characters that convert to HTML entities.
You may also like to read
- Remove String Spaces and Dash Using jQuery
- Delete Data From MySQL Database Using PHP
- PHP isset Function Used to Check Set Variables
I hope you like this tutorial on how to remove special characters from a string using PHP.
References
Grate post. Thank you for sharing the information. keep posting more post.