How to echo a paragraph text field using php

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by webjusti, Jan 21, 2014.

  1. webjusti

    webjusti New Member

    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    1
    Hi, I am using MySQL, Apache and have stored in it a paragraph text field. I need to echo this filed using php. When I use: <? echo { "$current_user->large paragraph" ; } ?> it gives me a string with no <br> breaks in it, just all the text in one block. How can I echo this paragraph text so that it will recognize the spaces and echo it correctly?

    Thanks, Carlos
     
  2. Anna

    Anna I am just me Staff Member

    Messages:
    8,366
    Likes Received:
    238
    Trophy Points:
    63
    Moved to "Scripts, 3rd Party Apps, and Programming" since this is a programming question.
     
  3. descalzo

    descalzo Grim Squeaker Community Support

    Messages:
    9,375
    Likes Received:
    327
    Trophy Points:
    83
    How are you generating the text?
    Yourself?
    User input?
    If your stored text has paragraph breaks stored as "\n" newlines, you will have to process the stored text to add the < br > tags.
    Or you can pre-process the text to add the < br > tags before you put it into the data base (makes for a more responsive display of the text).
     
    • Like Like x 1
  4. otakupag

    otakupag New Member

    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    You can use text or blob in data type. Then insert HTML code in it. You can use HTMLPurifier to avoid hackings. You can message me anytime for help :)
     
  5. ddnhf43

    ddnhf43 New Member

    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    1
    There is a built in function PHP that adds turns new lines into line breaks. Try this sample:

    Code:
    <?php echo nl2br($current_user->large paragraph); ?>
    In PHP, you should NEVER use short tags as it's a bad practice. You never know if a server has them enabled or not so I suggest getting into the practice of using the full tags. You also don't need the echo { "$current_user->large paragraph"; } as that's not needed, I suggest you take a look into learning the best practices of PHP. Hope I helped, feel free to message me if you need any help with PHP.
     
  6. Skizzerz

    Skizzerz Contributors Staff Member Contributors

    Messages:
    2,929
    Likes Received:
    117
    Trophy Points:
    63
    Short "echo" tags, e.g. <?= ?> are always available starting in PHP 5.4, so you can use those if you'd like. Beyond that, remember to always escape any user-provided strings before outputting them to the browser so that you are not vulnerable to HTML injection/XSS attacks. As @ddnhf43 suggested, learning some best practices for PHP is great, as it would help you avoid a lot of the pitfalls in the language
     

Share This Page