Allow Some Special Characters using Regex
The following regex matches any Unicode Letters 开发者_如何学编程+ Unicode Numbers + Vowel Signs + Dot + Dash + Underscore + Space
/^[\w\pN\pL\pM .-]+$/u
Works successfully.
I want to edit my regex so it accepts the following:
? ! ( ) % @ # , + - : newline
-
represents negative sign.
My attempt doesn't work:
/^[\w\pN\pL\pM .-**?!()%@#,+-:\r**]+$/u
Here is my snippet with latest attempt:
if(preg_match('/^[\w\pN\pL\pM .-?!()%@#,+-:\r]+$/u', $_POST['txtarea_msg']))
Any idea?
-
is a metacharacter in character classes, so you're saying:
blahblahblah all characters from .
to ?
blahblahblah all characters from +
to :
blah blah
It needs to be escaped with a \
: blahblah .\-? blahblah +\-: blahblah
/^[\w\pN\pL\pM \?!\(\)%@#,\+\-:\n\r]+$/u
should do it.
Some of these are regular expression characters, so you need to escape them:
/^[\w\pN\pL\pM .?!()%@#,+\-:\r]+$/u
Also note the difference between a newline (\n
) and carriage return (\r
).
精彩评论