hi guys! i need ur help because im noob at web programming.well.. i would like to insert formatted text (html) in the mysql database and retrive it to a page.
do i have to save text with all tags?
what is the best way to do that?

example: i hav a page,the index.php page...then theres is a text: blablablablabalbalba ...
i want to save it in the database and retrive it on the index page but i don't know how is the best way to store the text in the database with tags or not...thanks!!

Recommended Answers

All 5 Replies

Typically, you store the data in your database and insert it into a HTML template. You don't actually store the HTML with the data.

This does NOT include HTML that is actually a part of the data. Like, if a comment section of a blog allows users to use HTML tags to format their text, those would be saved in the database with the data. (Actually they would be a part of the data.)

Is that what you mean?

well...i have a text in my database,only the text without html,and i want to dispkay the same text on a page but formatting it with paragrapsh etc...how do i do that? thanks and excuse my english!

Member Avatar for rajarajan2017

Insert the data with tags and retrieve within echo of php.

Check out the nl2br function. It converts plain-text new-line characters to HTML <br> elements.

If you want to convert other plain-text characters to HTML chars (like spaces or tabs) you can use the str_replace function. Simply list the characters you want to replace as an array in the first parameter, their HTML counterparts in the second, and the text in the third.

$text = "Your      text,\n\t from\n\t\tthe\n\t\t\tdatabase.";

$plainChars = array("\n", " ", "\t");
$htmlChars = array('<br>', '&nbsp;', '&nbsp;&nbsp;&nbsp;&nbsp;');

$html = str_replace($plainChars, $htmlChars, $text);

This displays (in a browser):

Your text,
     from
        the
            database.

There is also the option of using the <pre> element, or the white-space: pre CSS element. They will both cause the browser to display the text within the element as-is, with the plain-text formatting intact.

ok guys thank you! its working!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.