Member Offer
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Getting line breaks to work in a textarea tag

Discussion in 'Website Design Forum:' started by Jazajay, Oct 31, 2010.

  1. Jazajay

    Jazajay Active Member

    Hi peeps,
    Now I have been trying to get this to work for some time and I appear to be a moron.

    Useful stuff to know.

    Program I am developing:
    Send an email form for a custom CMS.

    Problem with:
    Getting the signature to show exactly how it does when it will be sent in the textarea tag.

    Some code:
    <b>Your signature will be added to the end of this email 3</b>
    </p>
    <p>

    <?php
    $select="SELECT signature FROM sometable"; //Write the select from DB query
    $query=mysqli_query($connectionVariable,$select); //Query the DB
    $result=mysqli_fetch_assoc($query); //Get the results
    extract($result); //extract them so I can call them by their names
    $sig=nl2br($signature); //Add br tags to any line breaks
    $sig2=preg_replace("/<br \/>/","\n\n",$sig); //Remove the br tags and replace them with 2 new lines
    echo $sig;?> //Show the user the signature as it will appear.
    </p>
    <?php if(isset($_GET['sig'])){ //If the get variable is set to change the signature show the edit signature form.
    ?>
    <form action="" method="post">
    <p>
    <textarea name="sig" cols="10" rows="10"><?php echo $sig2;?></textarea> //Show a textarea with the db output in but with line breaks in instead of br tags
    </p>
    <p>
    <button type="submit">Change your signature</button>
    </p>
    </form>

    <?php }?> //end the if statement
    <p>
    <a href="admin/send-an-email/edit-your-signature">Edit your signature</a>
    </p>
    <p><button type="submit"><img src="img/send.jpg" alt="Send your email" /></button></p>
    </form>


    Right so my output is currently looking like this in the textarea tag:
    Kind regardsFirst Name Last NameTelephone: 01234567890Email....

    When it should be outputting as:
    Kind regards
    First Name Last Name
    Telephone: 01234567890
    Email....


    Now if I just add the br added variable it looks like:
    Kind regards<br />First Name Last Name<br />Telephone: 01234567890<br />Email....

    Which is not great for none techies. Also I don't like things that I can't do and I can't see why. :mad:

    Can someone explain my stupidity please as I'm at a loss. :(
     
  2. Levi

    Levi Moderator Staff Member

  3. Jazajay

    Jazajay Active Member

    Hi Levi,
    Thanks for looking.
    Yeah there has to be as that bit of preg_replace is a regular expression. The regular expression goes in between / / so if the regular expression you want to match also includes a backslash, as in <br />, to stop the expression terminating early it needs to be escaped with a forward slash, otherwise it wont match it.

    I'll have a look into that link, cheers dude. :)
     
  4. Jazajay

    Jazajay Active Member

    Yeah its weird their solution is to use \n which isn't working. I've done a lot of searching on this and just can get it to work. I have replace the \n now with the PHP constant PHP_EOL. So at least I've learned something new. :D

    The only thing that does is change the \n\n to \r\n if its on a windows system, other wise it should display \n, but still getting the same output:

    Here's the direct output on the user I'm currently working on:
    To the user above the textarea:
    Kind Regards
    Roger Needle.
    Office: 01213552605
    Mobile: 07941126852
    Email: Customer-enquires@rogerneedle.com
    Website: http://rogerneedle.com/
    Opening hours:
    08:00am to 20:00pm Monday through to Friday,
    08:00am to 13:00pm Saturdays,
    Sundays we are unfortunately closed.


    And in the textarea:
    Kind RegardsRoger Needle.Office: 01213552605Mobile: 07941126852Email: Customer-enquires@rogerneedle.comWebsite: http://rogerneedle.com/hours:08:00am to 20:00pm Monday through to Friday,08:00am to 13:00pm Saturdays,Sundays we are unfortunately closed.

    Really, really confused everything I have read says it should work. :confused:
     
  5. Mark Alexander

    Mark Alexander Senior Member

    Try using \r\n explicitly.

    PHP_EOL isn't for what you're trying to do. It works for the system PHP is running on, not the system of person who is viewing the webpage.

    What OS are you/your server using?
     
  6. Jazajay

    Jazajay Active Member

    Aww cheers. At the mo I'm on windows but it'll be on Linux. So
    1) Whats the best way to accomplish this.
    2) For everybody?

    Cheers for the time peeps,
    Much appreciated.
    Jaz
     
  7. Jazajay

    Jazajay Active Member

    Okay:
    Current code:
    extract($result);
    $sig=nl2br($signature);
    $sig2=preg_replace("/<br \/>/","\r\n",$sig);
    echo $sig;?>
    </p>
    <?php if(isset($_GET['sig'])){?>
    <form action="" method="post">
    <p>
    <textarea name="sig" cols="10" rows="10">
    <?php echo $sig2;?></textarea>


    Current output in the textarea tag:
    Kind RegardsRoger Needle.Office: 01213552605Mobile: 07941126852Email: Customer-enquires@rogerneedle.comWebsite: http://rogerneedle.com/opening hours:08:00am to 20:00pm Monday through to Friday,08:00am to 13:00pm Saturdays,Sundays we are unfortunately closed.

    Really confused. :confused:
     
  8. Mark Alexander

    Mark Alexander Senior Member

    Dunno, that code works for me perfectly.

    The only things I can think of is that the input doesn't have newline chars to begin with?
     
  9. Jazajay

    Jazajay Active Member

    Can you explain this for me please?
    As I'm pretty thick and lost by what you mean. :)
     
  10. Mark Alexander

    Mark Alexander Senior Member

    Well what is $sig when echoed out? Does it contain the <br /> tags as expected? If it does then the problem comes at preg_replace(), if it doesn't then it comes at nl2br() or before.
     
  11. Jazajay

    Jazajay Active Member

    Sorry mark, your time is appreciated. I've just been mega busy and as this is a freebie I haven't had time to get back to it. Will try tonight/tomorrow.

    Thanks for yours and everyones time it is highly apprecitated.
    Jaz : )
     

Share This Page