vwinstead Posted February 5, 2009 Share Posted February 5, 2009 I have stored the following into a database field: <p>this is a test</p> <br /> How do I display this using PHP so that it doesn't show the HTML formatting code, but instead just shows the HTML result as it is supposed to look? Quote Link to comment https://forums.phpfreaks.com/topic/143945-solved-displaying-html-code-from-php/ Share on other sites More sharing options...
flyhoney Posted February 5, 2009 Share Posted February 5, 2009 <?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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/143945-solved-displaying-html-code-from-php/#findComment-755323 Share on other sites More sharing options...
vwinstead Posted February 5, 2009 Author Share Posted February 5, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/143945-solved-displaying-html-code-from-php/#findComment-755343 Share on other sites More sharing options...
flyhoney Posted February 5, 2009 Share Posted February 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143945-solved-displaying-html-code-from-php/#findComment-755344 Share on other sites More sharing options...
vwinstead Posted February 6, 2009 Author Share Posted February 6, 2009 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 > means "greater than", the < 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 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? Quote Link to comment https://forums.phpfreaks.com/topic/143945-solved-displaying-html-code-from-php/#findComment-755902 Share on other sites More sharing options...
flyhoney Posted February 6, 2009 Share Posted February 6, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/143945-solved-displaying-html-code-from-php/#findComment-756035 Share on other sites More sharing options...
vwinstead Posted February 7, 2009 Author Share Posted February 7, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/143945-solved-displaying-html-code-from-php/#findComment-756645 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.