从MySql获取所有HTML到数组但不是已提供的代码

问题描述 投票:-1回答:1

我想从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?当我获得源视图时,我可以将它解雇。

php html mysql wordpress woocommerce
1个回答
0
投票

要转义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);

结果:https://3v4l.org/aD33U

Array
(
    [0] => Array
        (
            [post_content] => &lt;div id=&quot;foo&quot;&gt;&lt;/div&gt;
        )

    [1] => Array
        (
            [post_content] => &lt;div id=&quot;bar&quot;&gt;&lt;/div&gt;
        )

)

更新(删除所有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);

结果:https://3v4l.org/D1lfp

Array
(
    [0] => Array
        (
            [post_content] => Lenovo
        )

    [1] => Array
        (
            [post_content] => Dell
        )

)
© www.soinside.com 2019 - 2024. All rights reserved.