tags:

views:

67

answers:

4

Hello people, I use this for clean a string:

$clean = preg_replace("/[^a-zA-Z0-9\/_.;|+ -]/", '', $str);

Works good, but I need add into the regex the char '&' too, I tried to add:

$clean = preg_replace("/[^a-zA-Z0-9\/_.;&|+ -]/", '', $str);

or

$clean = preg_replace("/[^a-zA-Z0-9\/_.;\&|+ -]/", '', $str);

but this doesn't work, why? How can I do it? Big thanks.

+1  A: 

I'm not sure about php, but you can try to backslash the backslash: \\&

holub
hello holub, mmm.. seems doesn't work :(
Maiori
+2  A: 

i tried your pattern $clean = preg_replace("/[^a-zA-Z0-9\/_.;\&|+ -]/", '', $str); it seems to be working fine for me even without escaping it...

try this tool to test your regular expressions Regular expression tester

Pragati Sureka
Pragati Sureka
Looks like it works just fine
Robert Cabri
Nice tool ! I liked it!!!!!
santosh
A: 

[&] should be enough, see

http://www.regexp.net/?function=preg%5Fmatch&pattern=%5B%26%5D&string=Batman+%26+Robin

chelmertz
I do it, added [[
Maiori
You're already inside of `[]`, I meant your second alternative (in your question) should be fine.
chelmertz
A: 

Nothing guys, I tried your solutions but noone worked for me :/ I'm desperate :)

Maiori
What is `$str` (the input) when you're testing the regexp?
chelmertz