只允许英文字符/字母/数字和一些特殊字符[重复]

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>

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 !

Online demo.

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.