I have problems with removing special characters. I want to remove all special characters except "( ) / . % - &", because I'm setting that string as a title.
I edited code from the original (look below):
preg_replace('/[^a-zA-Z0-9_ -%][().][\/]/s', '', $String);
But this is not working to remove special characters like: "’s, "“", "â€", among others.
original code: (this works but it removes these characters: "( ) / . % - &")
preg_replace('/[^a-zA-Z0-9_ -]/s', '', $String);
Using str_ireplace() Method: The str_ireplace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “).
1 Answer. Show activity on this post. function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. return preg_replace('/[^A-Za-z\-]/', '', $string); // Removes special chars. }
Your dot is matching all characters. Escape it (and the other special characters), like this:
preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $String);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With