We are given a string and the task is to remove special characters from string str in PHP. Below are the approaches to remove special characters from string in PHP:
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 (” “).
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 program to Remove // Special Character From String // Function to remove the special 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; ?>