Guesser
Guesser

Reputation: 1857

PHP Accept line breaks using REGEX

I want to allow line breaks from a text area input in this regex but they are being removed?

$n= ereg_replace("[^a-zA-Z0-9[:blank:][:space:]&.\n\r\\/+-]+", "", $_REQUEST['input']);

Upvotes: 0

Views: 81

Answers (1)

Bart
Bart

Reputation: 17361

Use the /m modifier for your regex.

http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

m (PCRE_MULTILINE)

By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless D modifier is set). This is the same as Perl. When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.

Upvotes: 1

Related Questions