1

Is there a way to convert HTML string to JSON with PHP exactly like what toolslick.com html2json converter is doing.

This is an example of the html string

<html>
<body>
<table style="width: 100%">
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Age</th>
    </tr>
    <tr>
        <td>Jill</td>
        <td>Smith</td>
        <td>50</td>
    </tr>
    <tr>
        <td>Eve</td>
        <td>Jackson</td>
        <td>94</td>
    </tr>
</table>
</body>
</html>

I'm expecting a json like:

{
  "html": {
    "body": {
      "table": {
        "@style": "width: 100%",
        "tr": [
          {
            "th": [
              "Firstname",
              "Lastname",
              "Age"
            ]
          },
          {
            "td": [
              "Jill",
              "Smith",
              "50"
            ]
          },
          {
            "td": [
              "Eve",
              "Jackson",
              "94"
            ]
          }
        ]
      }
    }
  }
}

Any suggestion would be helpful thanks

4
  • You can use DOMDocument to parse it. Then write a recursive function that converts the document structure to JSON. Commented Apr 8, 2021 at 16:32
  • @Barmar I'm receiving the HTML as text on the backend Commented Apr 8, 2021 at 16:35
  • 2
    Right. The loadHTML() method will parse it Commented Apr 8, 2021 at 16:35
  • @Barmar Please any idea on the recursive function Commented Apr 8, 2021 at 17:51

1 Answer 1

3

If the HTML is valid you could try using SimpleXML and json_encode to parse it into JSON:

$xml = '<html>
<body>
<table style="width: 100%">
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Age</th>
    </tr>
    <tr>
        <td>Jill</td>
        <td>Smith</td>
        <td>50</td>
    </tr>
    <tr>
        <td>Eve</td>
        <td>Jackson</td>
        <td>94</td>
    </tr>
</table>
</body>
</html>';

$xmlObj = simplexml_load_string($xml);

echo json_encode($xmlObj);

https://3v4l.org/TZ4BP

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

2 Comments

Thank you much. It seems to be working on small snippet of HTML, however when a full html page is sent i get this error simplexml_load_string(): Entity: line 50: parser error : Opening and ending tag mismatch:
If you're getting that error, it must be invalid HTML. And no solution will work.

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.