How to Remove Special Characters From String in PHP

In this article, you will learn how to remove special characters from a string in PHP. Let’s say you have a string variable named ‘a’….

The post How to Remove Special Characters From String in PHP appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli

In this article, you will learn how to remove special characters from a string in PHP.

Let’s say you have a string variable named ‘a’.

// A string variable named 'a' with value "This string, contains special characters such as <;>"
$a = "This string, contains special characters such as <;>";

Remove Special Characters From String

In order to remove special characters from a string, you can use the str_replace() method.

// A string variable named 'a' with value "This string, contains special characters such as <;>"
$a = "This string, contains special characters such as <;>";

// Replace any special characters in string to whitespace
$result = str_replace(array( '"', ',' , ';', '<', '>' ), ' ', $a); 

echo "Result: ".$result;
// Result: This string contains special characters such as

Note: The str_replace() method functions by removing all special characters from the supplied string by replacing them with whitespaces.

The post How to Remove Special Characters From String in PHP appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli


Print Share Comment Cite Upload Translate Updates
APA

Ariessa Norramli | Sciencx (2021-03-01T12:46:20+00:00) How to Remove Special Characters From String in PHP. Retrieved from https://www.scien.cx/2021/03/01/how-to-remove-special-characters-from-string-in-php/

MLA
" » How to Remove Special Characters From String in PHP." Ariessa Norramli | Sciencx - Monday March 1, 2021, https://www.scien.cx/2021/03/01/how-to-remove-special-characters-from-string-in-php/
HARVARD
Ariessa Norramli | Sciencx Monday March 1, 2021 » How to Remove Special Characters From String in PHP., viewed ,<https://www.scien.cx/2021/03/01/how-to-remove-special-characters-from-string-in-php/>
VANCOUVER
Ariessa Norramli | Sciencx - » How to Remove Special Characters From String in PHP. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/01/how-to-remove-special-characters-from-string-in-php/
CHICAGO
" » How to Remove Special Characters From String in PHP." Ariessa Norramli | Sciencx - Accessed . https://www.scien.cx/2021/03/01/how-to-remove-special-characters-from-string-in-php/
IEEE
" » How to Remove Special Characters From String in PHP." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/03/01/how-to-remove-special-characters-from-string-in-php/. [Accessed: ]
rf:citation
» How to Remove Special Characters From String in PHP | Ariessa Norramli | Sciencx | https://www.scien.cx/2021/03/01/how-to-remove-special-characters-from-string-in-php/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.