Skip to content Skip to sidebar Skip to footer

PHP remove special characters in the string

Reading Time: < 1 minute

How to remove special characters in the string using php.This example it shows how to remove special characters from a string using regular expressions of php.

 
$string = 'Lorem ipsum 46=75dolor 45 Ggfd$#2sit am/et, cons^&*ect+etuer ad(ip)isci_ng el+it.'; 
$formattedString = preg_replace('#[^\w()/.%\-&]#'," ", $string); 
echo $formattedString; // Output //Lorem ipsum 4675dolor 45 Ggfd2sit amet cons&ectetuer ad(ip)isci_ng elit.