PDA

View Full Version : preg_match prevents hard break in textarea


songboy
11-20-2011, 04:34 PM
My script accepts textarea input unless someone adds a hard break to a line. It has to be connected to my preg_match because a preg_match warning is triggered. The preg_match is:

if(!preg_match("/^([0-9a-zA-Z' \:\;\,\.\!\?-]{2,800})$/", $blog_text))
{
$error_mess_blog = "Some blog characters used have not been accepted. Please try again.";

If a user uses a hard break and sends the data the text returns to what it was and the warning appears. It, seems the preg_match does not like the hard break. I'm assuming that when you press return an html break tag is registered (although it doesn't appear on the source). Is the preg_match objecting to the tag ? If it is, how do you get round the problem ? If it isn't, what can I do ?
Basically, this text area (the one used to post this message) is doing what my textarea can't.
Thanks in advance, for any answers.
Songboy

davidj
11-24-2011, 02:28 PM
have you tried using nl2br on the the contents before using the regex

songboy
11-25-2011, 08:49 PM
Hi dj and all.
Yes, nl2br was part of the solution.
The first thing was to stop the preg_match refusing a line break. I had to use this in the preg_match:

if(!preg_match("/^([0-9a-zA-Z' \:\;\,\.\!\?\r\n-]{2,800})$/", $blog_text))...notice the \r\n

Then, in the presentation I had to add this:

<div id="Layer25"><?php echo nl2br($blog_text) ?></div>
Now things are fine.
All the best -
Songboy