Skip to content

Instantly share code, notes, and snippets.

Created February 11, 2012 00:59
Show Gist options
  • Save anonymous/1794757 to your computer and use it in GitHub Desktop.
Save anonymous/1794757 to your computer and use it in GitHub Desktop.
Standard Deviation of HTML Tag Depth
<?php
function _getDomDepth(DomNode $node)
{
$r = -2;
while ($node)
{
$r++;
$node = $node->parentNode;
}
return $r;
}
$page = file_get_contents('http://apply.embed.ly/static/data/2.html');
libxml_use_internal_errors(true);
$document = new DOMDocument();
$document->loadHTML($page);
$dom = new DOMXPath($document);
$xmlNodes = $dom->query('//*');
$values = array();
$second_values = array();
$total = 0;
$second_total = 0;
foreach ($xmlNodes as $node)
{
if ($node->tagName == 'p')
$values[] = _getDomDepth($node);
}
foreach ($values as $v)
$total += $v;
$average = $total / count($values);
foreach ($values as $v)
$second_values[] = ($v - $average)*($v - $average);
foreach ($second_values as $sv)
$second_total += $sv;
$second_average = $second_total / count($second_values);
$stddev = sqrt($second_average);
print_r($stddev);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment