只允许英文字符/字母/数字和一些特殊字符[重复]
This question already has an answer here:
My problem is that I am making a small search engine from scratch, but it gets messed up if I search in Russian/any other language besides English. I was hoping some one could give me a code with regex that could filter out (not just detect, automaticallt filter out) Russian letters, or any other letters except the English letters, and keyboard special characters (-/:;()$&@". - etc). Later on, I will implement different language support for my engine, but for now, I want to finish the base of the engine.
Thanks in advance.
</div>
此问题已经存在 这里有一个答案: p>
-
php POST和非英语语言字符空白
2个答案\ r
span>
li>
- PHP:只允许字符串中的某些字符,而不使用正则表达式 1回答 span> li> ul> div>
我的问题是我正在做一个小的 搜索引擎从头开始,但如果我用俄语/任何其他搜索它会搞砸 r语言除了英语。 我希望有人可以给我一个带正则表达式的代码,可以过滤掉(不只是检测,自动过滤掉)俄文字母,或除英文字母以外的任何其他字母,以及键盘特殊字符( - /:;()$&amp; ; @“。 - 等等。”接下来,我将为我的引擎实现不同的语言支持,但是现在,我想完成引擎的基础。 p>
提前谢谢 。 p> div>
- PHP:只允许字符串中的某些字符,而不使用正则表达式 1回答 span> li> ul> div>
You may create an array of allowed characters and then filter those that are not allowed:
$allowed = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9), array(' ', '+', '/', '-', '*', '.')); // Create an array of allowed characters
$string = 'This is allowed and this not é Ó ½ and nothing 123.'; // test string
$array = str_split($string); // split the string (character length = 1)
echo implode('', array_intersect($array, $allowed)); // Filter and implode !
Why complicate? A regex will read the contents of the string, so better do it yourself. Read the characters of the string and check their corresponding ASCII value.
Create a hashset like structure with SplStorageObject and check manually if the characters fall in the desired set. You can add any characters that you want to read to this set.
EDIT - You might want to use regex too - something like [a-zA-Z0-9,./+&-] but using a set could allow you to expand your search engine gradually by adding more characters to the known-characters set.
this may not be the most effective way but it works :)
$str='"it is a simple test \ + - é Ó ½ 213 /:;()$&@".~" ';
$result= preg_replace('/[^\s\w\+\-\\":;@\(\)\$\&\.\/]*/', '', $str);
echo $result;
but you need to add every special characters.