We have given a string and the task is to remove special characters from a string str in PHP. In order to do this task, we have the following methods in PHP:
Method 1: Using str_replace() Method: The str_replace() method is used to remove all the special character from the given string str by replacing these character with the white space (” “).
Syntax:
str_replace( $searchVal, $replaceVal, $subjectVal, $count )
Example :
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; ?> |
Example to remove the Special Char
Method 2: Using str_ireplace() Method: The str_ireplace() method is used to remove all the special character from the given string str by replacing these character with the white space (” “). The difference between str_replace and str_ireplace is that str_ireplace is a case-insensitive.
Syntax:
str_ireplace( $searchVal, $replaceVal, $subjectVal, $count )
Example :
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; ?> |
Example to remove the Special Char
Method 3: 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 :
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; ?> |
Example to remove the Special Char
Recommended Posts:
- How to search special character in an angular smart table?
- How to remove the first character of string in PHP?
- How to remove portion of a string after a certain character in PHP?
- How to remove portion of a string after certain character in JavaScript ?
- How to remove a character from string in JavaScript ?
- Replace special characters in a string with underscore (_) in JavaScript
- Special Schemes of Node | URL.protocol API
- How to convert special characters to HTML in Javascript?
- Removing occurrences of a specific character from end of a string in PHP
- Remove new lines from string in PHP
- How to remove extension from string in PHP?
- How to remove line breaks from the string in PHP?
- How to remove all non-printable characters in a string in PHP?
- How to remove all leading zeros in a string in PHP ?
- How to remove control characters from PHP string ?
- How to remove all white spaces from a string in PHP ?
- How to remove white spaces only beginning/end of a string using PHP ?
- PHP | Remove duplicate elements from Array
- PHP | Ds\Vector remove() Function
- PHP | Ds\Vector remove() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

