Forum Moderators: coopster
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,
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;
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.
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].