1

Topic: Adding HTML tags to text stored in MySQL for proper display on webpage

Text for a webpage is stored in a MySQL database.  In the database, the text is grouped into paragraphs that are separated by linebreaks.  How do you take that text from the database and add the HTML tags so that it displays on the webpage properly?

adding <br /> for a single linebreak,

and adding
</p>
<p> for two linebreaks in a row.

If I'm going about this wrong then please correct me.  Basically what I'm trying to do is learn how to make my database text display on a webpage with the correct HTML tags.

Re: Adding HTML tags to text stored in MySQL for proper display on webpage

http://us2.php.net/manual/en/function.nl2br.php
Perhaps this? wink

3

Re: Adding HTML tags to text stored in MySQL for proper display on webpage

And after you have managed to convert \n to <br /> this may be of use to you
http://punbb.org/forums/viewtopic.php?id=8899
It turns viewtopics normal output into this

            <div class="postmsg">
                <h4> Re: New message indicators</h4>
                <p>This could be done on a session basis with JavaScript. It would be very similar to how it works now but with a slight change.</p>
                <p>In version 1.1.5, the unread image is sent to the browser like this...<br /><img src="img/Oxygen_new.png" width="16" height="16" alt=""></p>
                <p>But, when Pun prints these img tags, it could add the topic id as an attribute like this...<br /><img src="img/Oxygen_new.png" width="16" height="16" alt="" <strong>id="Topic5281"</strong>><br /><em>(PunBB would have to add "Topic" or "Forum" because ID's can't start with numbers)</em></p>
                <p>A JavaScript could then look for these, and note when a topic or all of the topics in a forum have been visited this session. If a topic was visited, the img with the matching id attribute could be set to <em>theImgTag.style.display='none'</em> with the JavaScript.</p>
                <p>This would turn off the new-message images for the topics and forums as you read them. And when you close the browser, the browser would automatically clear the session cookie.</p>
                <p><em>I know PunBB 1.2 is in feature freeze right now. So just consider this post to be purely academic for now.</em></p>
                <p class="lastedit">Last edited by ShawnBrown (09-12-2004 15:07:31)</p>
            </div>

4

Re: Adding HTML tags to text stored in MySQL for proper display on webpage

Oh, I see.  I already saw that thread but didn't know how to use it for my case.  Now I see the thing to do is to convert all the \n to <br />, then add the paragraphs -- two steps.  Thanks a lot.


Gosh, you're so smart.  Guess that's why they call you that.


This sounds easier to do that I expected it to be.  PHP continually amazes me at how easy it is to use.  I'll let you know how it goes.  Thanks again.

5 (edited by D9r 2005-09-28 01:31)

Re: Adding HTML tags to text stored in MySQL for proper display on webpage

Something like this, then, I suppose:

<p><?php 
$full_copy = $row[full_copy];
$full_copy = nl2br($full_copy);
$full_copy = preg_replace('#<br />\s*?<br />(?!\s*<br />)#i', "</p>\n<p>", $full_copy);
echo $full_copy;
 ?></p>

Re: Adding HTML tags to text stored in MySQL for proper display on webpage

Paul wrote:

And after you have managed to convert \n to <br /> this may be of use to you
http://punbb.org/forums/viewtopic.php?id=8899

So that's what it was for! big_smile

"Programming is like sex: one mistake and you have to support it for the rest of your life."

7

Re: Adding HTML tags to text stored in MySQL for proper display on webpage

us2.php.net/manual/en/function.nl2br.php wrote:

Note:  Starting with PHP 4.0.5, nl2br() is now XHTML compliant. All versions before 4.0.5 will return string with '<br>' inserted before newlines instead of '<br />'.

My version of MySQL is 4.0.17, but it still inserts '<br>'.  That's strange -- I thought it was supposed to be XHTML compliant.  I'll just add another line to replace the '<br>'s as well as the '<br />'s, to make the code forward-compatible.

Re: Adding HTML tags to text stored in MySQL for proper display on webpage

Or you can just do your own:

$text = str_replace("\n", "<br />\n", $text);

"Programming is like sex: one mistake and you have to support it for the rest of your life."