regular expression for a forum

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
etnastyles
Forum Newbie
Posts: 3
Joined: Mon Jul 07, 2008 4:26 am

regular expression for a forum

Post by etnastyles »

hello,

I am creating a small comment section for a small portal and was wondering what the best way of implementing regular expression is for a textarea lto allow users to eave comments.
Ideally i would allow anything that isnt code i.e. <img sr="" etc.. however my expression isnt workig very well
if fails on [enters] char(13) i was wondering if anyone has had this problem before and has a strong regular expression that would not break a database by allowing misc chard through, but at the same time allows users to type freely including £, !, & ? etc..

comment "/^[0-9A-Za-z_.!?, \s]{1,1125}$/"

regards

:)
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: regular expression for a forum

Post by ghurtado »

{1,1125}
Regular expressions are not very well suited for simple checks like string length. For that, I would use strlen
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: regular expression for a forum

Post by GeertDD »

Also, I definitely would not go about checking every entered character against an allowed character class. That is just slow and error-prone if you start taking into account accented characters and all that.

Rather, as ghurtado suggested, use strlen() to check the length of the message. I recommend mb_strlen(), though. You could continue to strip_tags() as well if you want.
Post Reply