Forum Moderators: coopster

Message Too Old, No Replies

Formating forms that post to mysql

Trying to wrap <p> tags where line breaks are found.

         

scraptoft

5:05 pm on Dec 6, 2007 (gmt 0)

10+ Year Member



I am trying to submit text into mysql via a php form. I need php to strip the line spaces and wrap <p></p> around new paragraphs.

I have post.php which posts to create.php (the code for create.php is below)

$body = $_POST['requiredbody'];

function nl2p($body)
{
// Use \n for newline on all systems
$body = preg_replace("/(\r\n¦\n¦\r)/", "\n", $body);

// Only allow two newlines in a row.
$body = preg_replace("/\n\n+/", "\n\n", $body);

// Put <p>..</p> around paragraphs
$body = preg_replace('/\n?(.+?)(\n\n¦\z)/s', "<p>$1</p>", $body);

// convert newlines not preceded by </p> to a <br /> tag
$body = preg_replace('¦(?<!</p> )\s*\n¦', "<br />", $body);

return $body;
}

All seams right to me - I just can't work out why it isn't working.

Regards,

gergoe

6:06 pm on Dec 6, 2007 (gmt 0)

10+ Year Member



I'd suggest this one, slightly changed, and fixed the major problems;

function nl2p($body) {
//
// Use \n for newline on all systems
$body = preg_replace("/((\r\n)¦\n¦\r)/", "\n", $body);
//
// Only allow two newlines in a row.
$body = preg_replace("/\n{2,}/", "\n\n", $body);
//
// Convert any single \n to a <br> (double \n are preserved)
$body = preg_replace('/(?<!\n)\n(?!\n)/', "<br />", $body);
//
// Put <p>..</p> around each single \n - as in the string
// now we should not have any single \n, only the left and right \n's
// of the double \n's will be matched.
$body = preg_replace('/(?:^¦\n)(.+?)(?:\n¦$)/s', '<p>$1</p>', $body);
return $body
}

What I had changed;

  • When dealing with multiple choice regular expressions, always enclose choices which are longer than one "character"; (abc¦def) will match "abcef" or "abdef", but not "abc" nor "def";
  • Exchanged the last two replaces, as it is more easier to get rid of the single \n's first and then the double ones, as in the other way around; With this change I could drastically simplify both regular expressions;

Please note that in the last replace, I match only one \n, otherwise the regular expressions would overlap, thus only each second double \n would have been matched. So for example the "\n\d\n" regular expression will match "\n1\n" and "\n3\n" from the "\n1\n2\n3\n" string, but the "\n2\n" will remain "unmatched".

scraptoft

6:51 pm on Dec 6, 2007 (gmt 0)

10+ Year Member



Hi, gergoe thanks for cleaning up my code and especially for explaining what you have changed.

I have updated my create.php however it is still not sending the $body with <p> tags to the mysql. I just can't figure out what is preventing it.

gergoe

2:03 am on Dec 7, 2007 (gmt 0)

10+ Year Member



Hope you are not just trying to use the content of the $body variable in your sql statement, without prior calling the nl2p() function (with the $body parameter)? I can't think of anything else, as the code I posted was tested beforehand (except a missing semicolon). Perhaps you should post the relevant parts of your code, or the result of your sql command (so it is clearly seen what is the result of the function).

scraptoft

1:08 pm on Dec 7, 2007 (gmt 0)

10+ Year Member



Hope you are not just trying to use the content of the $body variable in your sql statement, without prior calling the nl2p() function (with the $body parameter)

Embaressling enough I think I am..
I am using $body = $_POST['requiredbody']; to get the information from the form in post.php then I am using the code above.

I havn't used functions before so please excuse my lack of knowledge.

gergoe

1:59 pm on Dec 7, 2007 (gmt 0)

10+ Year Member



The
function function_name(parameters)
is only the declaration (followed by the implementation enclosed by curly brackets) of the function, whatever code you put into that function, it will only be executed when you call that function. In this case you declared a function named nl2p which has only one (incoming) parameter. The name of this parameter is $body, but you should understand that this name is only valid within the function, and it has nothing to do with the $body you declared outside of the function (outside of the scope of the function). So by naming both your variables $body, you caused your own confusion.

When calling a function you don't need to know what exactly the function does (or how does it do that), nor what the name of the parameters, only you need to pass the required parameters, in the case of nl2p, it is only one value (or variable), and then the function does its job, and returns the result. So making your original code working completely the following needs to be done:


$body_outside = $_POST['requiredbody'];
//
// Here goes the function (which is not called/executed yet)
function nl2p($body_private_to_function) {
//
// Use \n for newline on all systems
$body_private_to_function = preg_replace("/((\r\n)¦\n¦\r)/", "\n", $body_private_to_function);
//
// Only allow two newlines in a row.
$body_private_to_function = preg_replace("/\n{2,}/", "\n\n", $body_private_to_function);
//
// Convert any single \n to a <br> (double \n are preserved)
$body_private_to_function = preg_replace('/(?<!\n)\n(?!\n)/', "<br />", $body_private_to_function);
//
// Put <p>..</p> around each single \n - as in the string
// now we should not have any single \n, only the left and right \n's
// of the double \n's will be matched.
$body_private_to_function = preg_replace('/(?:^¦\n)(.+?)(?:\n¦$)/s', '<p>$1</p>', $body_private_to_function);
//
// Return the result
return $body_private_to_function;
}
//
// At this point the $body_private_to_function variable will contain nothing,
// eventually it does not exists, because it was defined in the function,
// and was valid within the function only. $body_outside will only contain
// the original value which we received from the html form. To get the desired
// result, we have to call our function now with the value of the $body_outside
// variable:
$processed_form_data = nl2p($body_outside);
//
// And only now we have the desired text in the $processed_form_data variable,
// so the following line will output the "htmlized" text to the browser:
echo $processed_form_data;
//
// Of course you can use different variable names, and you can also put the
// value of the $processed_form_data variable into database instead of sending back
// to the browser, it is up to you from now on.

I'd suggest you to read a bit more about functions [php.net], variables [php.net], and their scope [php.net].

scraptoft

3:56 pm on Dec 7, 2007 (gmt 0)

10+ Year Member



Not only is it now working, but I am so much more the wiser about php programming. Thanks for the generous support you gave to a new self taught phper.

Kind Regards,

gergoe

5:03 pm on Dec 7, 2007 (gmt 0)

10+ Year Member



You are welcome :-)