-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.

2
  • 1
    HTML will always be rendered unless you replace he markup brackets with < or > symbols. Commented Dec 28, 2017 at 22:12
  • thanks for the anwers: so that is stored: <td><b> Lenovo </b></td> but I get into my array only Lenovo - But I need all the Code. Commented Dec 28, 2017 at 22:18

1 Answer 1

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
        )

)
Sign up to request clarification or add additional context in comments.

5 Comments

Ok here I have found stackoverflow.com/questions/35218019/… - 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
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.
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.....
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. stackoverflow.com/questions/3577641/…
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.