Replacing certain characters while allowing unicode (PHP)
So I got a search box in a site we're developing that will search in a database with both English and Greek product strings. I'm trying to clear the text input from all kinds of special characters like: / . , ' ] [ % & _ etc. and replace them with a space or totally delete them. Even double instances of them should be deleted, like ^^, &&, [[ etc.
I have been messing around with preg_replace but can't find a solution...
开发者_Go百科Thanks in advance.
I finally came up with this:
$term = preg_replace("/[^\p{Greek}a-zA-Z0-9\s]+/u", '', $term);
It seems to work for what I need. It allows Greek characters (even with accents), alphanumerical and spaces. Replaces everything else with a space. Thanks for the fast response guys.
With preg_replace you can do what you are looking for. In the next example all non a-z nor A-Z, nor / _ | + - characters are replaced by '' (nothing, empty string)
preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $str);
add the characters you want to allow in that list and you will have your function.
Other way would be with str_replace()
but here you have to insert one by one all the elements that you want to remove in different function calls.
I hope it helps
Why not make an array and use str_replace?
$unallowedChars = array("^", "&", "/", "."); // more for your choosing
$searchContent = str_replace($unallowedChars, "", $searchContent);
Replaces all values of the array with "", in otherwords nothing.
精彩评论