Remove Special Character in PHP Using str_replace()

Today, I will discuss How to remove special characters from string in php using str_replace(), In order to do this task, The PHP have str_replace() function to remove special characters from a string str in PHP. 

Normally programmer are using regular expression to remove special character from string with help of preg_replace() method.

The preg_replace() method is a built-in function of PHP that also remove special character from string.

Let’s create php method to remove special characters from string.

Simple Example to Escape special characters in PHP

We ll use str_replace method to remove special characters from string, I have created a config.php file that will have escapeSequenceValidation variable to store all special characters, that need to remove from source string.



Remove Special Characters Using Regular Expression

We can also use preg_replace() method remove special character from string in php.It’s  used to perform a regular expression search and replace. This function searches for pattern in subject parameter and replaces them with the replacement.

The preg_replace() function returns an array if the subject parameter is an array otherwise it returns a string.

Output is –

Leave a Reply

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.