Jump to content

convert html entities back to normal character


caine

Recommended Posts

I have data ready in my mysql db, which are extracted from xml website. Therefore, the data are originally in html entities, but I want to display them in normal characters after retrieved them from mysql. It was unsuccessful attempt with the error statement :

Fatal error: Call to undefined function: htmlspecialchars_decode() in C:\Program Files\xampp\htdocs\search_results_new.php on line 90

Hopefully anybody can help with this.

[code]<html>
<head>
<title>Searching Results</title>
</head>
<body>

<?php



$display ="<h1>Searching Results</h1>";


//perform search
if(isset($_POST['submit']))
{
$db = mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("bulletin", $db) or die(mysql_error());

$searchWords = explode(" ", $_POST['textfield']);

foreach ($searchWords as $word)
{
  if ($word)
{
$whereParts[] = "`TITLE` LIKE '%{$word}%' OR `DEPARTMENT` LIKE '%{$word}%'" ;
}
}

$whereClause = implode(" OR ", $whereParts);

$query = "SELECT * FROM `bul_data` WHERE {$whereClause} ORDER by DATE"; 

$res = mysql_query($query) or die(mysql_error());

echo "query=".$query;

if(mysql_num_rows($res)>0)   
        {
             
                echo "<table width=\"100%\" border=\"1\">";
                echo "<tr>";
echo "<th>Date</th>";
                echo "<th>Title</th>";
                echo "<th>Department</th>";
                echo "<th>Link</th>";
                echo "</tr>";
       
                while($row=mysql_fetch_assoc($res))
                {
$date = $row['DATE'];
$date = decode($date);
               
$title = $row['TITLE'];
$title = decode($title);
               
                    $department = $row['DEPARTMENT'];
$department = decode($department);
               
                    $campus = $row['CAMPUS'];
$campus = decode($campus);
               

                    $link = $row['LINK'];
$link = decode($link);
               

                  $display .= "<tr>
<td><strong>$date</strong></td>
                                <td><strong>$title</strong></td>
                                <td>$department</td>
                                <td><a href = \"$link\">$link</a></td>
                      </tr>";
}

$display .="</table>";

    }
echo $display;
}

else
{
echo "No matches.";
}

function decode($text)
{
$text =  htmlspecialchars_decode($text,ENT_QUOTES);
return $text;

}

?>
</body>
</html>

[/code]

Are you sure you are using the version needed?
Run a file that contains:
[code]<?php echo phpversion(); ?>[/code]

Anyway, you can always use this function that I found in "User Contributed Notes" in htmlspecialchars_decode()'s page in the manual.
[code]<?php
if ( !function_exists('htmlspecialchars_decode') )
{
  function htmlspecialchars_decode($text)
  {
      return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
  }
}
?>[/code]


Orio.
I've noticed when parsing XML, that there are special characters not handled by the above mentioned php functions.  So, I ended up writing some code using preg_replace to handle these special characters.  I know it's not real clean, but it's easy to maintain as you find additional characters that need to be replaced in the XML feed.  Something like:

[code]
<?php
//get $input from your XML feed
$xml_char = array('/&#60;/','/&#039/'); //add encoded characters to this array
//note: the above array values should be the ampersand followed by the encoded value, but the forum seems to keep converting the ampersand to it's encoded equivalent...but, you get the picture.
$html_char = array("<","'"); //add corresponding decoded characters to this array
$output = preg_replace($xml_char,$html_char,$input);
?>
[/code]
  • 4 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.