对象属性如何转换为htmlentities


how object properties convert to htmlentities

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

$post_datas // this is where the query results saved
echo json_encode( (array)$post_datas );

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

JavaScript中有一个html实体,它被称为encodeURIComcomponent。也没有理由在json_encode中将对象假转换为数组。

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

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