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


Example 

Create file index.php and put the following code

<?php

$string = "text ! has \\ /allot # of % special % %*$&#*( characters";
$result = preg_replace('/[^a-zA-Z0-9_ -]/s', '', $string);
echo $result;
?>


Output:

text has allot of special characters