Доброго времени суток! Уважаемые форумчане, подскажите пожалуйста как мне быть: Пишу паука сборщика контента (для мини поисковика с целевым контентом) и столкнулся с проблемой поиска в теле страницы блоков с самым длинным текстом. Возможно кто-нибудь уже решал подобную задачу... Пытался решить проблему с помощью DOMDocument, и функции ( взята с php.net ) Но она выдает весь HTML целиком... Код | function getMaxTextBody($dom) { $content = $dom->getElementsByTagname('div'); $content2 = $dom->getElementsByTagname('td'); $content3 = $dom->getElementsByTagname('p');
$new = array(); foreach ($content as $value) { $new[] = $value; unset($value); } unset($content);
foreach ($content2 as $value) { $new[] = $value; unset($value); } unset($content2);
foreach ($content3 as $value) { $new[] = $value; unset($value); } unset($content3);
$maxlen = 0; $result = ''; foreach ($new as $item) { $str = $item->nodeValue; if (strlen($str) > $maxlen) { $content1 = $item->getElementsByTagName('div'); $content2 = $item->getElementsByTagname('td'); $content3 = $item->getElementsByTagname('p'); $contentnew = array(); foreach ($content1 as $value) { $contentnew[] = $value; unset($value); } unset($content1); foreach ($content2 as $value) { $contentnew[] = $value; unset($value); } unset($content2); foreach ($content3 as $value) { $contentnew[] = $value; unset($value); } unset($content3);
if (count($contentnew) == 0) { $result = $str; } else { foreach ($contentnew as $value) { $str1 = getMaxTextBody($value); $str2 = $value->nodeValue; //let's say largest body has more than 50% of the text in its parent if (strlen($str1) * 2 < strlen($str2)) { $str1 = $str2; } if (strlen($str1) * 2 > strlen($str) && strlen($str1) > $maxlen) { $result = $str1; } elseif (strlen($str1) > $maxlen) { $result = $str1; } $maxlen = strlen($result); } } $maxlen = strlen($result); unset($contentnew); } }
unset($new); return $result; }
|
Вознаграждение гарантирую
|