Remove Special Characters From String Using PHP

In this tutorial, learn how to remove special characters from string using PHP. Remove all special characters or only specified characters from a string with PHP. 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 remove special characters from string using PHP.

All Remove Special Characters From String Using PHP

If you want to remove all special characters from the string with PHP. You have to use the PHP preg_replace() which replaces or remove 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.

The above example contains the HTML strong tag and output contain no HTML entities. If you don’t want to remove HTML entities, you need to read the last topic of this tutorial.

Remove Specified Special Characters From String Using PHP

If you want to remove only the specified special characters from the string. You have to use the PHP str_replace() which replaces only the specified special characters. You may also like to read how to remove string spaces and dash using jQuery.

Remove single or multiple special characters from the string using this function.

Remove Single Specified Character from String
To remove the single special character from the string. 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 special character ‘&’ from the string with no space. The string contains the ‘&’ character and the output contains no special character ‘&’. If you want to remove a more special character from the string, read further.

Remove Multiple Specified Characters from String
In addition to the above example of removing the single character. You can remove multiple characters from the string also.

To remove the multiple characters from the string. Put all the special character 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 or Remove 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 which convert to HTML entities.

Hope, you like this tutorial of how to remove special characters from string using PHP. If you have any query regarding this tutorial, please comment below.

Also tell me, which method you are using to remove the special characters from the string.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.