From 4359f3c56c19494a8094c5e6a1b81de90b0b40c7 Mon Sep 17 00:00:00 2001 From: Andres Rey Date: Sat, 5 May 2018 10:50:59 +0100 Subject: Issue #63: Avoid diving by zero + test case --- src/Readability.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/Readability.php b/src/Readability.php index d69897b..b8b5b5b 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -983,7 +983,9 @@ class Readability // and whose scores are quite closed with current `topCandidate` node. $alternativeCandidateAncestors = []; for ($i = 1; $i < count($topCandidates); $i++) { - if ($topCandidates[$i]->contentScore / $topCandidate->contentScore >= 0.75) { + // In some cases we may end up with a top candidate with zero content score. To avoid dividing by zero + // we have to use max() and replace zero with a low value like 0.1 + if ($topCandidates[$i]->contentScore / max($topCandidate->contentScore, 0.1) >= 0.75) { array_push($alternativeCandidateAncestors, $topCandidates[$i]->getNodeAncestors(false)); } } -- cgit v1.2.3