HTML form format problems...

Jump to First Reply
Posted:
in Genius Bar edited January 2014
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?

Comments

  • Reply 1 of 4
    I have run into this issue, and didn't find an easy way around it. My sudo-hack was to take your input string and run it through a 'cleaning' function before inserting it into the DB. That cleaning function looks for all kinds of weird characters (like UTF-8 MS Word ones - those larger dashes, the triple dot, the curly quotes) and replaces them with ascii ones, and also looks for \

    \

    (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...) :



    Code:




    function doTextFormat($text, $paragraph = '') {

    $text = trim($text);

    $chars = array("\\?" => "&quot;", "\\"" => "&quot;", "\\?" => "&quot;", "?" => "-", "'" => "'", "?" => "'", "<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\\\0\\">\\\\0</a>", $text); // change any email address to a mailto: link



    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...
     0Likes 0Dislikes 0Informatives
  • Reply 2 of 4
    curiousuburbcuriousuburb Posts: 3,325member
    the smiley is a result of a : \\ code (without the space)
    Code:


    doesn't ignore \without space : \\with





    if you want to disable smilies, tick the "disable smilies in this post" box below the vB Code boxes
     0Likes 0Dislikes 0Informatives
  • Reply 3 of 4
    Your losing the line breaks because your not using nl2br. Simple as that.



    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.
     0Likes 0Dislikes 0Informatives
  • Reply 4 of 4
    Well technically if you were going standards compliant (as suggested by using <div>) then you shouldn't use nl2br, because line breaks are formatting which isn't handled by HTML. Replacing line breaks with <p> tags is standards compliant.



    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:



    Code:




    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

     0Likes 0Dislikes 0Informatives
Sign In or Register to comment.