我想从wordpress / woocommerce检索所有数据并做一些替换工作,但我不能得到真正的HTML并且只能在我的浏览器上呈现,也没有替代发生。这是我的代码:
$conn
是我与mysql的连接
$query = "SELECT post_content FROM wp_posts";
$result = $conn->query($query);
$data = mysqli_fetch_all($result,MYSQLI_ASSOC);
print_r($data); // this shows me all the data, but it is already processed and not the raw html which is stored into the database
如何从数据库中获取真正存储的html?当我获得源视图时,我可以将它解雇。
要转义HTML并停止渲染,请在值上使用htmlentities()。
您可以在输出时执行以下操作:
<?php
// perhaps in some loop
echo htmlentities($row['post_content']);
?>
或者首先循环它,如果你去print_r()它:/
<?php
$data = [
['post_content' => '<div id="foo"></div>'],
['post_content' => '<div id="bar"></div>'],
];
array_walk($data, function(&$value, $key) {
$value['post_content'] = htmlentities($value['post_content']);
});
print_r($data);
Array
(
[0] => Array
(
[post_content] => <div id="foo"></div>
)
[1] => Array
(
[post_content] => <div id="bar"></div>
)
)
更新(删除所有html标签)
<?php
$data = [
['post_content' => '<td><b> Lenovo </b></td>'],
['post_content' => '<td><b> Dell </b></td>'],
];
array_walk($data, function(&$value, $key) {
$value['post_content'] = trim(strip_tags($value['post_content']));
});
print_r($data);
Array
(
[0] => Array
(
[post_content] => Lenovo
)
[1] => Array
(
[post_content] => Dell
)
)