How do I add html_entity_decode to an array?

I am recording content with TinyMCE in codeigniter. However, the output source looks like this and does not show <and>. Instead, it renders HTML files such as & lessthan; and & more; etc.

The recording is done by the administrator after logging in.

The result comes from the database.

I chose escape in the model, but it still does the same.

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

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

Can anyone tell me how to do this?


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]);
    }
}

      

+2


source to share


2 answers


If I understand 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 understood the question correctly, which should do what you want.

+1


source


If you prefer to avoid looping over the result set, you can use the tools

array_map()

      



and follow these steps:

function fetch( $slug )
{
    $query = $this->db->query( "SELECT * FROM `pages` WHERE `slug` = '$slug'" );
    return array_map( array( $this, decodearray ), $query->result_array());
}

function decodearray( $myarray ){
    return html_entity_decode( $myarray,ENT_QUOTES );
}

      

0


source







All Articles