title: How to convert HTML to JSON using PHP?
body: <p>I can convert JSON to HTML using <a href="http://json2html.com/" rel="noreferrer">JsontoHtml</a> library. Now,I need to convert present HTML to JSON as shown in this site. When looked into the code I found the following script:</p>
  
  <pre><code>&lt;script&gt;
  $(function(){
  
      //HTML to JSON
      $('#btn-render-json').click(function() {
  
          //Set html output
          $('#html-output').html( $('#html-input').val() );
  
          //Process to JSON and format it for consumption
          $('#html-json').html( FormatJSON(toTransform($('#html-output').children())) );
      });
  
  });
  
  //Convert obj or array to transform
  function toTransform(obj) {
  
      var json;
  
      if( obj.length &gt; 1 )
      {
          json = [];
  
          for(var i = 0; i &lt; obj.length; i++)
              json[json.length++] = ObjToTransform(obj[i]);
      } else
          json = ObjToTransform(obj);
  
      return(json);
  }
  
  //Convert obj to transform
  function ObjToTransform(obj)
  {
      //Get the DOM element
      var el = $(obj).get(0);
  
      //Add the tag element
      var json = {'tag':el.nodeName.toLowerCase()};
  
      for (var attr, i=0, attrs=el.attributes, l=attrs.length; i&lt;l; i++){
          attr = attrs[i];
          json[attr.nodeName] = attr.value;
      }
  
      var children = $(obj).children();
  
      if( children.length &gt; 0 ) json['children'] = [];
      else json['html'] = $(obj).text();
  
      //Add the children
      for(var c = 0; c &lt; children.length; c++)
          json['children'][json['children'].length++] = toTransform(children[c]);
  
      return(json);
  }
  
  //Format JSON (with indents)
  function FormatJSON(oData, sIndent) {
      if (arguments.length &lt; 2) {
          var sIndent = "";
      }
      var sIndentStyle = "  ";
      var sDataType = RealTypeOf(oData);
  
      // open object
      if (sDataType == "array") {
          if (oData.length == 0) {
              return "[]";
          }
          var sHTML = "[";
      } else {
          var iCount = 0;
          $.each(oData, function() {
              iCount++;
              return;
          });
          if (iCount == 0) { // object is empty
              return "{}";
          }
          var sHTML = "{";
      }
  
      // loop through items
      var iCount = 0;
      $.each(oData, function(sKey, vValue) {
          if (iCount &gt; 0) {
              sHTML += ",";
          }
          if (sDataType == "array") {
              sHTML += ("\n" + sIndent + sIndentStyle);
          } else {
              sHTML += ("\"" + sKey + "\"" + ":");
          }
  
          // display relevant data type
          switch (RealTypeOf(vValue)) {
              case "array":
              case "object":
                  sHTML += FormatJSON(vValue, (sIndent + sIndentStyle));
                  break;
              case "boolean":
              case "number":
                  sHTML += vValue.toString();
                  break;
              case "null":
                  sHTML += "null";
                  break;
              case "string":
                  sHTML += ("\"" + vValue + "\"");
                  break;
              default:
                  sHTML += ("TYPEOF: " + typeof(vValue));
          }
  
          // loop
          iCount++;
      });
  
      // close object
      if (sDataType == "array") {
          sHTML += ("\n" + sIndent + "]");
      } else {
          sHTML += ("}");
      }
  
      // return
      return sHTML;
  }
  
  //Get the type of the obj (can replace by jquery type)
  function RealTypeOf(v) {
    if (typeof(v) == "object") {
      if (v === null) return "null";
      if (v.constructor == (new Array).constructor) return "array";
      if (v.constructor == (new Date).constructor) return "date";
      if (v.constructor == (new RegExp).constructor) return "regex";
      return "object";
    }
    return typeof(v);
  }
  &lt;/script&gt;
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/xwrXB.png" alt="enter image description here"></p>
  
  <p>Now, I am in need of using the following function in PHP. I can get the HTML data. All what I needed now is to convert the JavaScript function to PHP function. Is this possible? My major doubts are as follows:</p>
  
  <ul>
  <li><p>The primary input for the Javascript function <code>toTransform()</code> is an object. Is it possible to convert HTML to object via PHP?</p></li>
  <li><p>Are all the functions present in this particular JavaScript available in PHP? </p></li>
  </ul>
  
  <p>Please suggest me the idea.</p>
  
  <p>When I tried to convert script tag to json as per the answer given, I get errors. When I tried it in json2html site, it showed like this:<img src="https://i.stack.imgur.com/lvitt.png" alt="enter image description here"> .. How to achieve the same solution?</p>