for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Sioen;
/**
* Class HtmlToJson
*
* Converts html to a json object that can be understood by Sir Trevor
* @version 1.1.0
* @author Wouter Sioen <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php MIT
*/
class HtmlToJson
{
* Converts html to the json Sir Trevor requires
* @param string $html
* @return string The json string
public function toJson($html)
// Strip white space between tags to prevent creation of empty #text nodes
$html = preg_replace('~>\s+<~', '><', $html);
$document = new \DOMDocument();
// Load UTF-8 HTML hack (from http://bit.ly/pVDyCt)
$document->loadHTML('<?xml encoding="UTF-8">' . $html);
$document->encoding = 'UTF-8';
// fetch the body of the document. All html is stored in there
$body = $document->getElementsByTagName("body")->item(0);
$data = array();
// loop trough the child nodes and convert them
if ($body) {
foreach ($body->childNodes as $node) {
$toJsonContext = new ToJsonContext($node->nodeName);
$data[] = $toJsonContext->getData($node);
}
return json_encode(array('data' => $data));