HTML form format problems...
I'm using PHP and storing the jokes on a mysql database.
I'm making this "joke-a-day" web page, and when I add jokes, I just copy and paste from my list of jokes. I do all this in an html form, and then POST the <textarea>. I have the form page process itself and make the all the information in the parameter that holds the textarea information a string. I addslashes() and send it off to the database. When I get it, I stripslashes() and echo it. But it's doesn't keep it's original format with line breaks and returns. I guess I loose all that information when I pass it through POST, and my question is...is there anyway to NOT have it do this?
I'd rather have...
stuffbla. hstuffblahstuffblahst. uffblahstuffblahstuff
*notice the return*
stuffblahstuffblahstuffblahstuffblahstuffblahstuff blah
then...
stuffblahstuffblahstuffblahstuffblahstuffblahstuff blahstuffblahstuffblahstuffblahstuffblahstuffblahs tuffblah
It's REALLY annoying for long jokes, because it makes it almost impossible to read.
Basically, I just want it to keep the format that I put it in. How can I do that?
I'm making this "joke-a-day" web page, and when I add jokes, I just copy and paste from my list of jokes. I do all this in an html form, and then POST the <textarea>. I have the form page process itself and make the all the information in the parameter that holds the textarea information a string. I addslashes() and send it off to the database. When I get it, I stripslashes() and echo it. But it's doesn't keep it's original format with line breaks and returns. I guess I loose all that information when I pass it through POST, and my question is...is there anyway to NOT have it do this?
I'd rather have...
stuffbla. hstuffblahstuffblahst. uffblahstuffblahstuff
*notice the return*
stuffblahstuffblahstuffblahstuffblahstuffblahstuff blah
then...
stuffblahstuffblahstuffblahstuffblahstuffblahstuff blahstuffblahstuffblahstuffblahstuffblahstuffblahs tuffblah
It's REALLY annoying for long jokes, because it makes it almost impossible to read.
Basically, I just want it to keep the format that I put it in. How can I do that?
Comments
\
(return, new line). I take my insert string and add a <p> to the beginning of it - then every time I hit a \
\
(or multiple of that using regex), I replace that \
\
with </p><p>. At the end of the string I enter add an </p>.
It is a hack I admit, but it keeps the formatting nice and clean when it comes out of the DB...
Here's the function I was talking about (if it has a second parameter of "p" passed to it, it adds the <p> and </p> - I use this function to clean any text passed to the DB and some of those don't want <p> tags because they will screw up formatting...) :
function doTextFormat($text, $paragraph = '') {
$text = trim($text);
$chars = array("\\?" => """, "\\"" => """, "\\?" => """, "?" => "-", "'" => "'", "?" => "'", "<b>" => "<span class=\\"b\\">", "<i>" => "<span class=\\"i\\">", "<u>" => "<span class=\\"u\\">", "</b>" => "</span>", "</i>" => "</span>", "</u>" => "</span>");
$text = strtr($text, $chars); // strip out bad chars and replace returns with </p><p> tags to autoformat the text
if ($paragraph == 'p') {
$text = '<p>' . $text;
$text = eregi_replace("[\
\
\
]+", "</p><p>", $text);
$text = eregi_replace("[\
\
]+", "</p><p>", $text);
$text = eregi_replace("\
+", "</p><p>", $text);
$text = $text . '</p>';
}
$text = eregi_replace("www.[^<>[:space:]]+[[:alnum:]/]", "<a href=\\"http://\\\\0\\">\\\\0</a>", $text); // change any reference to www.someting to a url link
$text = eregi_replace("[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\\\\.[a-z]{2,4}", "<a href=\\"mailto
return $text;
}
Hmmm - looks like it took my "and QUOT semi-colon" and replaced them with real quotes - I thought the CODE tag was supposed to keep everything real? I also like the smiley in the middle of my CODE...
doesn't ignore
if you want to disable smilies, tick the "disable smilies in this post" box below the vB Code boxes
You don't need a huge function, just nl2br. Even if you did use that giant function above I would switch it to a div.
That function above is doing a lot of things besides looking for line breaks - it cleans out UTF-8 tags (which some browsers choke on), it replaces URLs with links - it replaces email addresses with mailto: links.
If you want just to replace line breaks, and the input text is always coming from a known source (like not from different document formats and platforms), you could always use a slightly less 'giant' function:
function cleanText($text) {
$text = '<p>' . $text;
$text = eregi_replace("[\
\
]+", "</p><p>", $text);
$text = $text . '</p>';
return $text;
}
That will keep you standards compliant and shouldn't strain your bandwidth