Debug or search alternative preg_replace

The question was asked: 6 years 9 months ago   views: 7
0

Found out about the function preg_replace.

$zamena = "123";
$aaa = "12";
$aaa = preg_replace('/[^'.$zamena.']/', ", $aaa);
echo $aaa; // result - "12;" should be "12" // it worked

$zamena = "qwertyuiopasdfghjklzxcvbnmQWERTYuiopasdfghjklzxcvbnmейцукенгшщзхъфывапролджэячсмитьбюейцукенгшщзхъфывапролджэячсмитьбю1234567890@#$%&()-=+?,.! ";
$aaa = "123 ^^ : ;";
$aaa = preg_replace('/[^'.$zamena.']/', ", $aaa);
echo $aaa; // result is "123 ^^ : ;" should be "123" // how so?

As can be seen from the second code block, the function is somehow not working completely. In the description of this function did not find a single word about ";" and ":". Explain why these signs function not weeds? And not weeds out many others.

P. S. This design is intended from $aaa marks to leave only those that are in the $zamena. In $zamena usually Russian and English alphabet + numbers + some symbols that are sometimes standard and sometimes not.

Asked: Max Max 21-06-2012 в 13:48:34

Answers   1

0

You need to escape the special characters with preg_quote()

$aaa = preg_replace('/[^'.preg_quote($zamena).']/', ", $aaa);

Or manually. So I advise to read the documentation on regular expressions and design [] in particular.

Answered: Ilya Pirogov Ilya Pirogov 21-06-2012 в 13:55:36
thank you. and for the answer, and in what direction smoke 8-) - Sergey Sergey 21-06-2012 в 14:08:46