Remove Special Characters From String Using PHP

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 Special Characters From String Using 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.

Output

This isnt my strongboldstrong string Im working

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.

Output

This isn’t my bold string. I’m working.

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.

Output

This isn’t my strongboldstrong string. &I’m working.

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.

Output

This isn’t my <strong>bold</strong> string. &I’m working.

The above example contains 3 special characters that convert to HTML entities.

You may also like to read

I hope you like this tutorial on how to remove special characters from a string using PHP.

References

One reply on “Remove Special Characters From String Using PHP”

Comments are closed.