Forum Moderators: coopster
eregi is deprecated, use preg_match instead.
I don't know what you mean by "all other symbols" as I don't even know what country you are in, you may have a different keyboard. So I'll just throw "&" and "$" in there, you extend the class as you like.
What you want is a character class, defined by the brackets. []. A leading carat in the class means "anything that is NOT these characters. [^]. + means "one or more of these." i makes it case insensitive. - means a range, as in a through z, so if you want a literal dash you need to escape it.
if (preg_match('/[^a-z0-9\+\-\&\$]+/i',$string)) {
echo " $string contains characters that are not in the acceptable set.";
}
/ and ending / - Delimiter for the regular expression. This can be anything, using the perl-ish delimiters.
[] - defines a character class. That is, anything in the brackets is a character to match on. So [abc] will match onthe characters a, b, and c only. a-z, with the dash, defines a range.
^ - when immediately after the first [ in a class, this a not operator. It means anything that is NOT these characters. To add a carat (^) as one of the class members, don't make it the first item and escape it.
+ - one or more of the previous characters. Otside a class, as in a+, would mean one or more of the letter "a". Others are *, meaning zero or more, or numerical ranges, like a{2,4}, meaning anywhere from 2 to 4 "a"'s in succession.
i - case insensitive, otherwise you'd have to do A-Za-z. Note that with the i operator, you don't have to do A-Za-z, either /[A-Z]+/i or /[a-z]+/i will match beause it's case insensitive.
Also note that 0-9 and \d are identical:
/[0-9]+/
/[\d]+/
So to answer the question, add the characters you need. If it errors, add a backslash to escape the character (you will need this with < and > as those are special characters). In PHP, the \ character is a slippery one, if it's in your class you need FOUR of them.
if (preg_match("/[\/\\\\]+/",$some_var)) .....
'/[^a-z0-9\+\-\&\$\(\)\[\]\{\}\!\/\?\.\,\<\>\'"\;\:\^\%\#\-\=\+]+/i'
(Some escaping in this sample might not be necessary, but it doesn't hurt . . . )
Although I will say, not all of these should be allowed. You should apply htmlentities() for <,>,&, and some of the others, and swap % for the word 'percent'. % is one of the ways to circumnavigate filters and inject into a database using encoded characters. [ and ] are familiar patterns of link-dropping in spamming forms using BBcode style links, also a good reason to kick them out.
What is the "? Regexp modifiers are supposed to follow the delimiter,
/[pattern]/[modifiers]
and will definately error.
Not sure what you mean by "mistake." I'm using the single quote ' as the PHP delimiter for the regexp pattern and regexp delimiter,
'/my pattern is here/'
so if that's to be part of the class, it needs escaping
'/[\']+/'
where the double quote should not, so
'/[\'"]+/i'
should have worked. How that came to be a superscript 1 is beyond me. :-P
If you changed quoting (which is maybe why you have ") you needed only to escape the double quote instead.
"/['\"]+/i"
also A-za-z is not necessary with the i modifier, but it's an identical result so . . . whatever works.