PHP:将html_entity_decode应用于整个结果数组


PHP : applying html_entity_decode to whole result array

为了安全起见,所有插入MySQL数据库的数据都会用"html_sanitizer"清理。

使用PDO:检索数据

$statement = $dbconn->prepare("SELECT * from table");
$result = $statement->fetchAll(PDO::FETCH_ASSOC);

为了在页面上正确显示文本,我使用这个:

echo(html_entity_decode($result[0]['content'], ENT_COMPAT, 'UTF-8'))

这一切都是正确的。

我的问题是,将"htmlentity_decode"应用于数组的所有内容的最佳方式是什么?

我希望有这样的东西:

$statement = $dbconn->prepare("SELECT * from table");
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
$result = html_entity_decode($result, ENT_COMPAT, 'UTF-8'); // here is the bit i'm looking for
echo ($result[0]['content'])

非常感谢您的建议。

似乎您希望在数组的每个字段中应用html_entity_decode()。如果是这样,那么array_walk_recurive()最适合此目的。

Example:
$result = array(); // Say Array of your result
array_walk_recursive($result , 'stripFunction');
public function stripFunction(&$item, $key)
{
    $item = html_entity_decode($item, ENT_COMPAT, 'UTF-8');
}

它递归地将用户函数应用于数组的每个成员。http://php.net/manual/en/function.array-walk-recursive.php是文档的参考链接。