Howto Remove Specific, Special Characters From String In PHP
To remove specific/special characters from a string in PHP; This tutorial demonstrates to you, how you can remove special or specific characters from string in PHP. When you work with PHP or any other PHP framework like laravel, Codeigniter, Cake PHP, etc. Many times you want to remove special or specific character from a string
Remove Special or Specific Characters From a String In PHP
This tutorial explains to you, how you can easily remove special or specific characters from string in PHP. We will show you two methods to remove the special or specific character from string with or without using a regular expression.
Str_replace PHP Function
The str_replace() function finds characters in a string and replaces some characters with some other characters in a string.
You can use PHP function str_replace() to remove special characters or specific characters from string in PHP.
Method 1: Remove Specific Character from String
We have one string if you want to remove specific characters from a string. So use the below function:
The above example removes “World” to
inspirasiprogrammer.id. When you pass any string in this function, this function removes find “World” in a given string and replace it to “
inspirasiprogrammer.id”. And return a new string.
Method 2: Remove Special Character From String
If you want to remove special character (#,@,%,&,*, etc) from string in PHP. You can use the below function for that:
The above function will remove all the special characters from the given string in PHP. If you want to add more any special character for remove in the given string, you can do it easily.
Preg_replace PHP Function
The PHP preg_replace() function is used to perform a regular expression for search and replace the content.
preg_replace( $pattern, $replacement, $subject, $limit, $count );
Example 1: Remove Special Character From String Using Regular Expression
You can use the preg_replace function to remove the special character from the string using regular expression in PHP.
In this tutorial, you have learned, how you can easily remove special characters or specific characters in the string using the PHP function str_replace() and preg_replace().