Conditions | 3 |
Paths | 2 |
Total Lines | 25 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
22 | public function toJson($html) |
||
23 | { |
||
24 | // Strip white space between tags to prevent creation of empty #text nodes |
||
25 | $html = preg_replace('~>\s+<~', '><', $html); |
||
26 | $document = new \DOMDocument(); |
||
27 | |||
28 | // Load UTF-8 HTML hack (from http://bit.ly/pVDyCt) |
||
29 | $document->loadHTML('<?xml encoding="UTF-8">' . $html); |
||
30 | $document->encoding = 'UTF-8'; |
||
31 | |||
32 | // fetch the body of the document. All html is stored in there |
||
33 | $body = $document->getElementsByTagName("body")->item(0); |
||
34 | |||
35 | $data = array(); |
||
36 | |||
37 | // loop trough the child nodes and convert them |
||
38 | if ($body) { |
||
39 | foreach ($body->childNodes as $node) { |
||
40 | $toJsonContext = new ToJsonContext($node->nodeName); |
||
41 | $data[] = $toJsonContext->getData($node); |
||
42 | } |
||
43 | } |
||
44 | |||
45 | return json_encode(array('data' => $data)); |
||
46 | } |
||
47 | } |
||
48 |