Page 1 of 1

regular expression for a forum

Posted: Mon Aug 18, 2008 10:16 am
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

:)

Re: regular expression for a forum

Posted: Mon Aug 18, 2008 12:37 pm
by ghurtado
{1,1125}
Regular expressions are not very well suited for simple checks like string length. For that, I would use strlen

Re: regular expression for a forum

Posted: Tue Aug 19, 2008 3:27 am
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.