Forum Moderators: coopster

Message Too Old, No Replies

line break differences from textarea/MySQL?

         

Readie

5:22 pm on Feb 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I (finally) got around to coding my preview button, but I had to do it in a seemingly convoluted way.

INSERT INTO page_preview...
SELECT * FROM page_preview...
DELETE FROM page_preview...

The reason for this is
/[\r\n]{2,}?/m
wasn't recognising line breaks straight from a textarea, so an input like this:

Oranges

And

Apples

Came out like this:

Oranges\r\n\r\n\r\n\r\nAnd\r\n\r\n\r\n\r\nApples

With my preg_replace failing to pick up on those \r\n's... I'm wondering if anyone could explain why inserting it into the database makes all the difference?

rocknbil

8:27 pm on Feb 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not fully following . . . but let's see. :-P

Is this literally what's stored, as in, you can see the backslashes and r/n's?

Oranges\r\n\r\n\r\n\r\nAnd\r\n\r\n\r\n\r\nApples

If that's the case, I'd say you're applying some PHP function (mysql_real_escape_string()?) before the preg. Put the preg first?

Also I'm not sure what you're doing with the preg . . . but observe the following.

/[\r\n]{2,}?/m

The second quantifier ? limits greedy pattern matching, I am guessing you can dispense with it completely. In that context (I think, untested) it means "zero or ONE of the preceding pattern of 2 to infinity newlines/returns." A quantifier on the quantifier. :-)

I probably shouldn't comment further until I have a full grasp of the context, which I vaguely recall being discussed . . . but has been pushed out of my head for other stuff.

Readie

9:41 pm on Feb 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, directly echoing form the textarea, so:

$content = mysql_real_escape_string($_POST['content']);
$content = preg_replace($symb, $repl, $content);
echo $content;


Results in all the \r\n being echoed.

$content = mysql_real_escape_string($_POST['content']);
$prev_ins_sql = "INSERT INTO post_preview (n_content) VALUES('$content')";
mysql_query($prev_ins_sql);

$prev_ret_sql = 'SELECT * FROM post_preview WHERE n_content="' . $content . '"';
$prev_result = mysql_query($prev_ret_sql);
$prev_content = preg_replace($symb, $repl, mysql_result($prev_result,0,"n_content"));
$prev_id = mysql_result($prev_result,0,"n_id");

$prev_del_sql = 'DELETE FROM post_preview WHERE n_id="' . $prev_id . '"';
mysql_query($prev_del_sql);

echo $prev_content;


Displays absolutley fine. I just find it strange since I'm not making any changes to the input, merely shuffling it around a bit.

And I was wondering why I was getting more line breaks than I was wanting -.- I really need to get around to properly learning regex.

rocknbil

2:33 am on Feb 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think if you do this,

$test = preg_replace($symb, $repl, $_POST['content']);
echo "After regex, before MRES $test <br>";
$content = mysql_real_escape_string($_POST['content']);
echo "After MRES $content";

It may become clear. See, your regex is not looking for \'s in the pattern, and MRES is escaping them. I think. :-)

In a regex, this is not a literal:

preg_match("/\n/",$something);

Nor is this:

preg_match('/\n/',$something);

Those are representations of white space newlines.

When you have to use double quotes and match on a literal \, oddly enough, you have to do this


if (preg_match("/[\/\\\\]+/",$var)) { $inputError .= '<li>You cannot use slashes \ or / in the title.</li>'; }

So I guess your choices are, preg it out as a \n\r before MRES or preg it as a literal after.

Readie

3:27 am on Feb 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ahh, I see. It never even occured to me that MRES might be causing this; I've become so engrained in the habit of applying MRES to everything I retrieve via $_POST/GET/REQUEST.

Just tested it by preg'ing it, then escaping it and it works as you thought :) So thanks very much - that should take a bit of a load off the server.