对象属性如何转换为htmlentities

时间:2015-02-12 04:59:44

标签: php

我使用PDO在mysql中创建了一个查询,并使用FETCH_OBJ将数据作为对象返回, 我想将它打印为Ajax调用的JSON,我在stackoverflow中也看到了一个例子并且它正在工作。 这是代码。

$post_datas // this is where the query results saved

echo json_encode( (array)$post_datas );

但我的问题是我想让每个属性转换为htmlentities,因为javascript中没有htmlentities函数。是否有快捷方式使每个属性转换为htmlemtities或者我需要创建一个循环并在执行json_encode之前将每个属性设置为html属性?

1 个答案:

答案 0 :(得分:0)

JavaScript中有一个html实体,它叫做encodeURIComonent。也没有理由假冒对象作为json_encode中的数组。

此外,如果您想这样做,则没有预定义的功能。您需要循环遍历属性,并在每个属性上执行html实体

foreach($postDatas as $key => $val){
    $postDatas->$key = htmlentities($val);
}