Forum Moderators: coopster

Message Too Old, No Replies

escaping chars in an array

easy question

         

WhosAWhata

5:04 pm on Jul 5, 2004 (gmt 0)

10+ Year Member



which of these that i missed need to be escaped?

$a = array('~','1','2','3','4','5','6','7','8','9','0', '-','=','`','!','@','#','$','%','^','&','*','(',')', '_','+','Q','W','E','R','T','Y','U','I','O','P','{','}', '¦','A','S','D','F','G','H','J','K','L','\'','Z','X','C', 'V','B','N','M','[',']','\\',';','\'','q','w','e','r','t', 'y','u','i','o','p','a','s','d','f','g','h','j','k','l', 'z','x','c','v','b','n','m','<','>',',','.','/','?');

thanks

[edited by: jatar_k at 8:30 pm (utc) on July 6, 2004]
[edit reason] fixed sidescroll [/edit]

coopster

5:15 pm on Jul 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Not sure I understand the question exactly, but why not use preg_quote() [php.net]?

The special regular expression characters are: . \\ + *? [ ^ ] $ ( ) { } =! < > ¦ :

WhosAWhata

6:15 pm on Jul 5, 2004 (gmt 0)

10+ Year Member



i know i have to escape (\) a lot of chars so php doesn't try to associate any special traits with them

i have escaped some but still get a few
Warning: ereg(): REG_EPAREN
warnings

do you have a list of all chars that need to be escaped in an array?

WhosAWhata

6:18 pm on Jul 5, 2004 (gmt 0)

10+ Year Member



so far i have

$a = array('~' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '0' , '-' , '=' , '`' , '!' , '@' , '#' , '\$' , '%' , '^' , '&' , '\*' , '(' , ')' , '_' , '\+' , 'Q' , 'W' , 'E' , 'R' , 'T' , 'Y' , 'U' , 'I' , 'O' , 'P' , '{' , '}' , '¦' , 'A' , 'S' , 'D' , 'F' , 'G' , 'H' , 'J' , 'K' , 'L' , '\'' , 'Z' , 'X' , 'C' , 'V' , 'B' , 'N' , 'M' , '[' , ']' , '\\' , ';' , '\'' , 'q' , 'w' , 'e' , 'r' , 't' , 'y' , 'u' , 'i' , 'o' , 'p' , 'a' , 's' , 'd' , 'f' , 'g' , 'h' , 'j' , 'k' , 'l' , 'z' , 'x' , 'c' , 'v' , 'b' , 'n' , 'm' , '<' , '>' , ',' , '\.' , '/' , '\?');

coopster

6:26 pm on Jul 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yeah, they are listed in msg#2, which came from the preg_quote() link.

WhosAWhata

6:37 pm on Jul 5, 2004 (gmt 0)

10+ Year Member



i've done similar things and never had to escape { or } in the array

i am using the EREG function not PREG all i need (i think) is to escape them for the ereg function, i don't think it is as picky as preg

coopster

8:20 pm on Jul 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm still a bit confused by your request. An array value can be of any PHP type, including type
string
. When you set a value as a string [php.net], the only character you'll need to escape will be the backslash itself since it will occur before the single quote which is at the end of the string -- '\\'. To get the single quote character into your array, just use double quotes around that particular entry -- "'"

For what it's worth, Perl compatible regex functions are usually faster than the POSIX regex functions. POSIX regex's do indeed include braces and brackets as special characters, so they would need to be escaped as well.