php Escape all characters before comparing

Go To


I need some effective way to escape all special characters in string.For example, my string

I need some effective way(1) [2] for $10 + e

should be converted in

I need some effective way\(1\) \[2\] for \$10 \+ e


2012-04-03 22:09
by Milos
have you tried using the escape/addslashes function - Ibu 2012-04-03 22:11
addslashessavinger 2012-04-03 22:11
addslahes only handles single quote, double quote, backslash and NUL bytes. He seems to be escaping anything that's non-alphanum or space - webbiedave 2012-04-03 22:12
@savinger add slashes isn't what milos wants - hjpotter92 2012-04-03 22:14
What special characters do you want escaped? Usually one escapes special characters for transportation, like SQL-queries or URL's. The set of special characters therefore depends on the environment, the string will be interpreted in. There are many predefined escaping functions in php like mysql_real_escape_string and rawurlencode - Basti 2012-04-03 22:22


Try quotemeta:

2012-04-03 22:14
by Pete
wow, didn't know about it. : - hjpotter92 2012-04-03 22:16
Obviously need to be careful what context this is being used in though - not a good idea to use for escaping for database or shell arguments. There's much better ways to both those things - Pete 2012-04-03 22:18
This is what i was looking for. I hope it will work :) Thx mat - Milos 2012-04-04 09:59