PHP regex to allow newline didn't work -
php preg_match take new line
i want pass every post/string through php preg_match function. want take alpha-numerics , special characters. help me edit syntax allow newline. users fill textarea , press enter. next syntax not allow new line.
please feedback whether next special characters done or not */_:,.?@;-*
if (preg_match("/^[0-9a-za-z \/_:,.?@;-]+$/", $string)) { echo 'good'; else { echo 'bad'; }
you there!
the dotall modifier mentioned others irrelevant regex.
to allow new lines, add together \r\n character class. code becomes:
if (preg_match("/^[\r\n0-9a-za-z \/_:,.?@;-]+$/", $string)) { echo 'good'; else { echo 'bad'; } note test , regex can written in tidier way:
echo (preg_match("~^[\r\n\w /:,.?@;-]+$~",$string))? "***good!***" : "bad!"; see result of online demo @ bottom.
\w matches letters, digits , underscores, can rid of them in character class changing delimiter ~ allows utilize / slash without escaping (you need escape delimiters) php regex
No comments:
Post a Comment