Jump to content

Properly render htmlspecialchars as HTML


howardmoore

Recommended Posts

Hi,

 

I am a very new to HTML, so please go easy on the explanations. I have tried searching the forum but unfortunately I didn't understanding much of what I read.

 

I have a PHP script that writes to a MySQL database. One particular row ('description') uses HTML, as I use fckeditor to create the content. fckeditor and the form I wrote posts the content to the database just fine using:

 

$description=htmlspecialchars($description,ENT_QUOTES);

 

When I browse the row using phpMyadmin the htmlspecialchars has worked perfectly. However, the problem I have is when I am trying to show the contents of 'description' on a webpage. I use the following:

 

<?php

$sql = "SELECT * FROM `jmnpevents`
          WHERE startDate between '".date("Y-m-d")."' AND '".date("Y-m-d",mktime(0, 0, 0, date("m"), date("d")+$daysAhead, date("Y")))."' 
          ORDER BY startDate ASC";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
	if ($row["description"]<>'') {
		echo '<table class="calendar_list"><tr><td class="calendar_list_date">';
		echo date("F j, Y",strtotime($row["startDate"])).'  -  <strong>';
		echo nl2br(stripslashes(utf8_decode($row["name"]))).'</strong></td></tr>';			
		echo '<tr><td class="calendar_list_description">';
		echo stripslashes(utf8_decode($row["description"])).'</td></tr></table><br /><br />';
	};
};
?>

 

However, this only shows the content as HTML tags, rather then rendering it properly as HTML. Does anyone know what I can do to solve this problem. I presume that it is something to do with the decoding, but for the life of me the searches I have done have not shed any light.

 

Thanks in advance.

Neil

Link to comment
Share on other sites

htmlspecialchars_decode()

 

Thanks - I did see this in some of the searches I did, but I cannot work out where this will fit into my code. I have tried replacing utf8_decode with htmlspecialchars_decode, but this just returns no results at all.

 

Thanks,

Neil

Link to comment
Share on other sites

When using utf8_decode I get:

 

<p><span style="color: #0000ff">Coloured Blue</span></p> <p><span style="color: #00ff00">Coloured Green</span></p>

 

When using htmlspecialchars_decode() I get nothing at all. The page just stops at the point where it is meant to display, meaning that the rest of the page template failes to display too.

Link to comment
Share on other sites

When using utf8_decode I get:

 

<p><span style="color: #0000ff">Coloured Blue</span></p> <p><span style="color: #00ff00">Coloured Green</span></p>

 

When using htmlspecialchars_decode() I get nothing at all. The page just stops at the point where it is meant to display, meaning that the rest of the page template failes to display too.

 

Is an error printed when using htmlspecialchars_decode()? Turn error_reporting on:

 

error_reporting(E_ALL); //put this at the top of your script

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.