-1

I want to retrive all Data out of wordpress /woocomerce and do some replace jobs, but I cant get the real HTML and get only rendered on my browser, also no replace happen. Here my code:

$conn is my connection to 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

How do I get the real stored html out of the database? I can sea it when I get the source view.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Marius
  • 15
  • 1

1 Answers1

0

To escape the HTML and stop it rendering, use htmlentities() on the value.

You can do this when outputting:

<?php
// perhaps in some loop 
echo htmlentities($row['post_content']);
?>

Or loop over it first, if your going to print_r() it :/

<?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);

Result: 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;
        )

)

Update (strip all html tags)

<?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);

Result: https://3v4l.org/D1lfp

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

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

)
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • Ok here I have found https://stackoverflow.com/questions/35218019/how-to-render-html-tags-from-mysql-database-in-php - that is quite answering my question in theory, but how can i get that raw html into my array now, since I have catched it all. `echo $data['post_content'];` that didnt work, array is NULL – Marius Dec 28 '17 at 22:34
  • That's the opposite of what your asking lol, if you use strip_tags() it will strip the tags from the html is that what you want? See update. – Lawrence Cherone Dec 28 '17 at 22:38
  • sorry it gets confusing. I want not to strip anything and not to render the code out of the table, I just want it to show like it does when i do the query inside the db directly, with all tags and so on..... – Marius Dec 28 '17 at 22:42
  • Like the first, im confused can you update your question with examples, or your HTML and your expected result, if you dont want to strip tags and dont want to prevent the tags from rendering, I suspect your need to parse the HTML instead which then you should use DOMdocument or some other parser. https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php – Lawrence Cherone Dec 28 '17 at 22:45
  • `array_walk($data, function(&$value, $key) { $value['post_content'] = htmlentities($value['post_content']); });` that has done the trick, sorry I had an error in the code! i am really thankfull! great! – Marius Dec 29 '17 at 14:25