Jump to content

[SOLVED] displaying html code from PHP


vwinstead

Recommended Posts

<?php
$string = '<p>this is a test</p> <br />';
echo $string; // this will print the HTML, and it will be rendered by the browser

$string = '<p>this is a test</p> <br />';
echo htmlspecialchars($string); // this will print the HTML entities, and it will be displayed by the browser
?>

Link to comment
Share on other sites

<?php
$string = '<p>this is a test</p> <br />';
echo $string; // this will print the HTML, and it will be rendered by the browser

$string = '<p>this is a test</p> <br />';
echo htmlspecialchars($string); // this will print the HTML entities, and it will be displayed by the browser
?>

 

For some reason that displayed the following:

<p>This is a test test</p> <br />

 

Looks really messed up.  Does that mean there's some hidden formatting text in there?  Here's some more specifics on what I'm doing:

1.  regular text stored in a MySQL field is pulled onto a web page using php

2.  visitor clicks an 'edit' button and is able to edit the contents on that page.

3.  page contents are now shown in a text editor box for editing (using FCKeditor)

4.  pressing the 'submit' button sends the altered version of the text back to the database for future display on the web page.

 

I think the FCKeditor is altering all the text so that it shows up with all the special characters. 

Link to comment
Share on other sites

I think the FCKeditor is altering all the text so that it shows up with all the special characters. 

 

Looks like that may be the case.  Make sure the text in your database is actual HTML and not entities (> etc..)

 

When you see something like > on the frontend, that means that the HTML is being escaped twice.

Link to comment
Share on other sites

Looks like that may be the case.  Make sure the text in your database is actual HTML and not entities (> etc..)

When you see something like > on the frontend, that means that the HTML is being escaped twice.

 

The &gt means "greater than", the &lt means "less than".  Now I see what it's doing.  There is a break in the chain somewhere along the line.  Text is getting properly sent over to the FCKeditor, but the editor automatically shows it as HTML and then when you're done editing it, the edited text gets saved with HTML special characters (like </p>) so it will be formatted properly for the browser.  But when I retrieve the text again I'm not displaying it as HTML, I'm displaying it as straight text.  Then when I edit it again, this straight text gets the special characters added to it again.  It's a viscious cycle  :P

 

The problem is partly that I think the format might be wrong on the MySQL database for this filed.  Currently I'm using BLOB and maybe it should be something else like LARGETEXT or whatever. 

 

The other problem is that I don't know how to display html on my page using php.  The HTML code is stored in the MySQL database -> I'm calling it with echo.  This just shows whatever is there as text on the screen.  I COULD use htmlspecialchars to get rid of the </p> and all that, but that means the formatting will not exist when displayed in the browser. 

 

Isn't there a way to keep all the formatting elements in the database and use php to display the text properly in the browser? 

Link to comment
Share on other sites

Isn't there a way to keep all the formatting elements in the database and use php to display the text properly in the browser? 

 

Definitely, this is done all the time.  You just need to make sure that the html is not being converted to entities.  My personal preference is to save raw user input.  This means save the text to the database exactly as it was entered.  (this might require tweaking some settings in FCKEditor).  And yes, I would switch from a BLOB to a TEXT.  They are pretty much the same thing but one is for binary data and the other for text.  So when you grab the text out of the database, you can choose to filter it however you want, ie htmlspecialchars, striptags etc..

Link to comment
Share on other sites

I figured it out!  I had to run html_entity_decode on the content that I pull out of MySQL before sending it to the FCKeditor.  In fact, any time I pull data out of the database that is formatted as HTML code I need to decode it first with html_entity_decode before displaying on the page.  Thanks for the help! 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.