Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

I need help with displaying newline or paragraph using perl on my website

by 5plit_func (Beadle)
on Jun 28, 2014 at 22:13 UTC ( [id://1091575]=perlquestion: print w/replies, xml ) Need Help??

5plit_func has asked for the wisdom of the Perl Monks concerning the following question:

Perlmonks, i am developing a web application using CGI and DBD on my localhost. i have successfully inserted data into the database that is a text field with data. My problem here is when i retrieve the data from the database and display it using Perl there are no newlines displayed on the page the text is all joined together. This is a sample code to demonstrate what i am trying to do.
print('<p>'.$text . '</p>');
I do not know what function to use that will help me display it paragraph by paragraph of newline by newline. Your help will be highly appreciated. Regards. 5plit_func
  • Comment on I need help with displaying newline or paragraph using perl on my website
  • Download Code

Replies are listed 'Best First'.
Re: I need help with displaying newline or paragraph using perl on my website
by thomas895 (Deacon) on Jun 28, 2014 at 22:25 UTC

    If I understand correctly, your data in the database is text, with newline characters in it, and you wish to display that data with the newlines in it, as one would expect a text editor to do.

    If this is the case, you have two options.

    Option 1

    Use a <pre /> block, like so:

    print "<pre>$text</p>";

    Note that it is advised to escape the text so that there is no possibility of XSS or conflicts with HTML special characters.
    CGI can do that:

    print "<pre>", CGI::escapeHTML($text), "</pre>";

    Option 2

    To use the HTML paragraph block, simply use a regular expression to replace all newline characters with the HTML <br />.
    Regular expressions are a very important part of the Perl language, and therefore left as an exercise to the reader. ;-)

    ~Thomas~ 
    "Excuse me for butting in, but I'm interrupt-driven..."

      I have tried option one and it did not work. I am not so good at regular expression although i am learning gradually about it.

      Please are there resources online where i can get help or read about what i need to do in other to display paragraphs as i did while inserting text into a database using Perl.

        Depends on what you mean by "get help" -- if you mean 'do it for you,' hire a programer. If you mean help fix your code, you'll have to show it to us, and not just say "it did not work."

        But if you mean 'get help learning how to....' -- well, http://www.perlmonks.com/index.pl?node=Tutorials#Pattern-Matching-Regular-Expressions-and-Parsing is just awaiting your visit.

        And in that same vein, I'm not clear what you $text looks like, coming out of the database, so can't even guess why the snippet you show does "not work."

        However, another and better option might be to stick the text into the db with html markup...

        <p>para 1</p> <p>Slightly more extensive para two:</p> <ol><li>with details</li> <li>with details</li> </ol> <p>Pare 3 loret ipsum etc....</p>

        That:

        para 1

        Slightly more extensive para two:

        1. with details
        2. with details

        Pare 3 loret ipsum etc....

        combined with whatever escaping demanded by your code, should do the job.

        BTW, re the first reply, above, <br /> is NOT para code as the node might seem to suggest. It's an entity which produces a single linebreak (like 0d, 0a in a windows file) but not a para break which puts in an extra linebreak so paras are separated by a line blank except for the newline.


        check Ln42!

Re: I need help with displaying newline or paragraph using perl on my website
by AppleFritter (Vicar) on Jun 29, 2014 at 09:54 UTC

    My problem here is when i retrieve the data from the database and display it using Perl there are no newlines displayed on the page the text is all joined together.

    Newlines -- as represented by \n -- are considered whitespace like any other in HTML and rendered as such, rather than as actual newlines (outside of tags such as <pre> and <code>, anyway). You need to turn them into <br> tags:

    $text =~ s#\n#<br>#g;

    These'll indicate that the browser should render linebreaks.

Re: I need help with displaying newline or paragraph using perl on my website
by Anonymous Monk on Jun 28, 2014 at 22:26 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1091575]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found