By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,745 Members | 1,889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,745 IT Pros & Developers. It's quick & easy.

The woes of a PHP form submission

P: n/a
Good evening to the skilled Ladies and Gentlemen of the PHP world. I
am fairly new to all of this but learning quickly. I have run in to a
need to remove blank lines from a form submission after its sent back
to the web server for processing by a PHP document.

I have a textarea that users can paste lines of text in to and submit
them for inclusion in to a database. Every aspect is working
perfectly, except one. No matter what I try, I cannot get rid of
simple blank lines from the form submission.

$theLines = $HTTP_POST_VARS['userSubmitted'];

This variable ($theLines) now contains the contents from the textarea.
I have tried using ...

$theLines = explode("\n", $theLines);

and

$theLines = explode("\r", $theLines);

But all of the blank lines are still there. If anyone has some
snippet that they would share, I would be very grateful.

With great respect,

Martin
Jul 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Martin the Mac Addict wrote...
[...]
But all of the blank lines are still there. If anyone has some
snippet that they would share, I would be very grateful.


<?php
function remove_blank_lines($txt) {
str_replace("\r", '', $txt); ## remove all "\r"s
## and convert all double enters (and triple ...) to one enter
while (strpos($txt, "\n\n") !== false)
$txt = str_replace("\n\n", "\n", $txt);
return $txt;
}
?>

NOTE: this was typed into google without being checked.
Jul 16 '05 #2

P: n/a
ad***@minoguetech.com (Martin the Mac Addict) wrote in message
news:<e4************************@posting.google.co m>...

I have a textarea that users can paste lines of text in to and submit
them for inclusion in to a database. Every aspect is working
perfectly, except one. No matter what I try, I cannot get rid of
simple blank lines from the form submission.


This may be somehow related to the end-of-line character issue.
End of line in DOS/Windows is a two-character sequence "\r\n",
in Unix, a single character "\n", and in MacOS, a single character
"\r". So if you want to strip end-of-line characters from a form
submission, you should try something like this:

$theLines = $HTTP_POST_VARS['userSubmitted'];
$theLines = str_replace("\n", '', $theLines);
$theLines = str_replace("\r", '', $theLines);

Also, take a look at the script you use to render the submission.
If you have something like this:

<textarea>
<?php
echo $theLines;
?>

</textarea>

then the extra blank line actually comes from your HTML, not your
PHP...

Cheers,
NC
Jul 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.