In this tutorial, We will inform you how to remove special character from string in PHP. we want to remove all special characters from the string by using preg_replace. So, its function will remove any special character except letter and numbers.
some data that contains special characters (e.g. single quote, double quote. special characters removed from a string using the preg_replace function.
<?php
$string="example?";
$data = preg_replace('/[^a-zA-Z0-9_ -]/s','',$string);
echo $data;
//output example
?>
some data that contains special characters (e.g. single quote, double quote. special characters removed from a string using the str_replace function.
<?php
function deleteSpecialChar($str) {
// replace all special characters by empty string
$res = str_replace( array( '%', '@', '\'', ';', '<', '>' ), ' ', $str);
return $res;
}
// string example
$str = "A % B @ C 'E;";
// Call the function
$res = deleteSpecialChar($str);
// Display the result
echo $res;
//output A B C D E
?>