Search code examples
phpcodeigniterhtml-encode

How to add html_entity_decode to array?


I am making a content entry with TinyMCE in codeigniter. However the output source is like the following and does not show < and >. Instead it shows HTML enties like &lessthan; and &greaterthan; etc.

The entry is made by admin after logged in.

Output comes from database.

I took out escape in model, but it still does the same thing.

Also I have a config setting, $config['global_xss_filtering'] = FALSE;

So I want to add html_entity_decode. But the $page_data is an array. The array has id, title, content and slug which is used for page item.

Could anyone tell me how to do it please?


Output example:

&lt;p&gt;&lt;img src=&quot;images/icon1.png&quot; border=&quot;0&quot;
alt=&quot;icon&quot; width=&quot;48&quot; height=&quot;48&quot; /&gt;
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Model code:

<?php

class Pagemodel extends Model 
{
....
...

/** 
* Return an array of a page — used in the front end
*
* @access public
* @param string
* @return array
*/
function fetch($slug)
{
    $query = $this->db->query("SELECT * FROM `pages` WHERE `slug` = '$slug'");
    return $query->result_array();
}


...
...

}

?>

Controller code:

function index()
{
    $page_slug = $this->uri->segment('2'); // Grab the URI segment

    if($page_slug === FALSE)
    {
        $page_slug = 'home';
    }

$page_data = $this->pages->fetch($page_slug); // Pull the page data from the database

    if($page_data === FALSE)
    {
        show_404(); // Show a 404 if no page exists
    }
    else
    {
        $this->_view('index', $page_data[0]);
    }
}

Solution

  • If I got you correctly you want to pass 'html_entity_decode.' to all fields that are returned from your database. You can easily add something to your fetch function:

    function fetch($slug)
    {
        $query = $this->db->query("SELECT * FROM `pages` WHERE `slug` = '$slug'");
        for($i=0; $i<$query->num_rows(); $i++)
        {
            $html_decoded[$i]['id'] = html_entity_decode($query->id);
            $html_decoded[$i]['title'] = html_entity_decode($query->title);
            $html_decoded[$i]['content'] = html_entity_decode($query->content);
            $html_decoded[$i]['slug'] = html_entity_decode($query->slug);
        }
    
        return  $html_decoded;
    }
    

    If I got your question right that should do what you want.