How to Remove Special Character from String in PHP ? - GeeksforGeeks

02 Dec, 2021
In this article, we will see how to remove special characters from strings in PHP, along with understanding their implementation through the examples. We have given a string and we need to remove special characters from string str in PHP, for this, we have the following methods in PHP:
Using str_replace() Method: The str_replace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “).
Syntax:
str_replace( $searchVal, $replaceVal, $subjectVal, $count )
Example: This example illustrates the use of the str_replace() function to remove the special characters from the string.
PHP
<?php  // PHP program to Remove   // Special Character From String    // Function to remove the spacial   function RemoveSpecialChar($str) {        // Using str_replace() function       // to replace the word       $res = str_replace( array( '\'', '"',      ',' , ';', '<', '>' ), ' ', $str);        // Returning the result       return $res;      }    // Given string  $str = "Example,to remove<the>Special'Char;";     // Function calling  $str1 = RemoveSpecialChar($str);     // Printing the result  echo $str1; ?>
Output:
Example to remove the Special Char
Using str_ireplace() Method: The str_ireplace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “). The difference between str_replace and str_ireplace is that str_ireplace is case-insensitive.
Syntax:
str_ireplace( $searchVal, $replaceVal, $subjectVal, $count )
Example: This example illustrates the use of the str_ireplace() method to remove all the special characters from the string.
PHP
<?php  // PHP program to Remove   // Special Character From String    // Function to remove the spacial   function RemoveSpecialChar($str){        // Using str_ireplace() function       // to replace the word       $res = str_ireplace( array( '\'', '"',      ',' , ';', '<', '>' ), ' ', $str);        // returning the result       return $res;      }    // Given string  $str = "Example,to remove<the>Special'Char;";     // Function calling  $str1 = RemoveSpecialChar($str);     // Printing the result  echo $str1; ?>
Output:
Example to remove the Special Char
Using preg_replace() Method: The preg_replace() method is used to perform a regular expression for search and replace the content.
Syntax:
preg_replace( $pattern, $replacement, $subject, $limit, $count )
Example: This example illustrates the use of the preg_replace() method where the particular or all the matched pattern found in the input string will be replaced with the substring or white space.
PHP
<?php  // PHP program to Remove   // Special Character From String    // Function to remove the spacial   function RemoveSpecialChar($str){        // Using preg_replace() function       // to replace the word       $res = preg_replace('/[^a-zA-Z0-9_ -]/s',' ',$str);        // Returning the result       return $res;  }    // Given string  $str = "Example,to remove<the>Special'Char;";     // Function calling  $str1 = RemoveSpecialChar($str);     // Printing the result  echo $str1; ?>
Output:
Example to remove the Special Char
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
bhaskargeeksforgeeks
PHP-Questions
PHP-string
PHP
Web Technologies
Web technologies Questions
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to execute PHP code using command line ?
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
PHP in_array() Function
How to pop an alert message box using PHP ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?