Reputation: 807
I have a regular expression which allows only certain characters in the message. What I'm trying to achieve is to allow line breaks as well which will be preserved using nl2br function.
That's what I have so far:
preg_replace('/[^a-zA-Z0-9ńśćółęążź\ .,-]/','',$message)
As far as I have checked, the following expression should preserve line breaks but I'm having problems adding it to the above expression:
/(\r|\n|\r\n){2,}/
Upvotes: 2
Views: 2165
Reputation: 1987
I believe this is what you are looking for.
print preg_replace('/' . preg_quote('[^a-z0-9ńśćółęążź\ .,-\r\n])') . '/i','',"test\r\ntest\rtest\ntest");
Upvotes: 1
Reputation: 1881
You would just want to add \r
& \n
to the list of characters to not replace. So:
preg_replace("/[^a-zA-Z0-9ńśćółęążź\ \.,\-\r\n]/",'',$message)
In the above expression I've also had to change the '
to " (so that \r
& \n
are recognised) but have also had to escape the .
and -
characters
Upvotes: 3