如何将 html_entity_decode 添加到数组?

发布于 2024-08-06 04:16:56 字数 1494 浏览 4 评论 0原文

我正在 codeigniter 中使用 TinyMCE 制作内容条目。 但是输出源如下所示,并且不显示 <和>。相反,它显示 HTML 实体,例如 &lessthan;和&大于;等等。

该条目是由管理员登录后输入的。

输出来自数据库。

我在模型中取出了 escape,但它仍然做同样的事情。

我还有一个配置设置 $config['global_xss_filtering'] = FALSE;

所以我想添加html_entity_decode。但 $page_data 是一个数组。 该数组包含 id、标题、内容和用于页面项目的 slug。

谁能告诉我该怎么做吗?


输出示例:

&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.

型号代码:

<?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();
}


...
...

}

?>

控制器代码:

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

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:

<p><img src="images/icon1.png" border="0"
alt="icon" width="48" height="48" />
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]);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖兔^ω^ 2024-08-13 04:16:56

如果我没猜错的话,你想传递“html_entity_decode”。从数据库返回的所有字段。您可以轻松地向您的获取函数添加一些内容:

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

少女情怀诗 2024-08-13 04:16:56

如果您希望避免在结果集上循环,您可以使用 的功能

array_map()

并执行如下操作:

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

If you prefer avoid cycling on the resultset, you can use the facilities of

array_map()

and do something like this:

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 );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文