Forum Moderators: coopster

Message Too Old, No Replies

need help with preg_match problem

         

demid

6:48 am on Nov 27, 2009 (gmt 0)

10+ Year Member



Hello!can anybody give me a precise ereg expression which check only for:

1.) alphabets only
2.) for numeric only (includes "+" and "-")
3.) all other symbols (such as /,.*? and so on) on keyboard

i really need this but i don't know how to write.
thanks...

please don't post links.. thanks..

rocknbil

9:46 pm on Nov 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm come looking for help but demand how one helps you . . . . well, welcome aboard anyway.

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.";
}

demid

8:47 am on Nov 29, 2009 (gmt 0)

10+ Year Member



First of all...excuse me for my english - it' a reason of question's form =)
I'm from russia. I mean i need to write preg_match function for:
1) a-zA-Z
2)0-9
3)for other symbols: ()[]{}!/?.,<>'";:&^%$#-=+\¦space
I can't understand how to write the rule for them.
If it is not very hard can you write the rule for my symbols please.I need to make application form (and i made it) but this is my stumbling stone.

rocknbil

7:00 pm on Nov 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You would just add the characters as needed. An analysis of the regexp is

/ 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.

demid

7:35 pm on Nov 29, 2009 (gmt 0)

10+ Year Member



thank you
but you have a mistake
'" will not work....i did right variant
/[^0-9a-zA-Z_\+\-\=\.\&\$\[\]\{\}\,\.\<\>\(\)\^\¹\#\:\;\?\!\/\*\%\s]+/&quot;

rocknbil

8:21 pm on Nov 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



/[^0-9a-zA-Z_\+\-\=\.\&\$\[\]\{\}\,\.\<\>\(\)\^\¹\#\:\;\?\!\/\*\%\s]+/&quot;

What is the &quot;? 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 &quot;) 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.