summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/HTMLParser.php28
2 files changed, 16 insertions, 13 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 229e111..9f0a6e6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
- Fix "Unsupported operand types" (PR #31)
- Fix division by zero when no title was found (issue #32)
- New function to retrieve all images at once (PR #30)
+- Get the title from the `<title>` tag before searching on the `<meta>` tags
## [v0.3.0](https://github.com/andreskrey/readability.php/releases/tag/v0.3.0)
diff --git a/src/HTMLParser.php b/src/HTMLParser.php
index 9c570ba..69e11f1 100644
--- a/src/HTMLParser.php
+++ b/src/HTMLParser.php
@@ -128,8 +128,6 @@ class HTMLParser
$this->metadata['image'] = $this->getMainImage();
- $this->metadata['title'] = $this->getTitle();
-
// Checking for minimum HTML to work with.
if (!($root = $this->dom->getElementsByTagName('body')->item(0)) || !$root->firstChild) {
return false;
@@ -479,20 +477,24 @@ class HTMLParser
$metadata['excerpt'] = $values['twitter:description'];
}
- if (array_key_exists('og:title', $values)) {
- // Use facebook open graph title.
- $metadata['title'] = $values['og:title'];
- } elseif (array_key_exists('twitter:title', $values)) {
- // Use twitter cards title.
- $metadata['title'] = $values['twitter:title'];
- }
+ $metadata['title'] = $this->getTitle();
- if (array_key_exists('og:image', $values) || array_key_exists('twitter:image', $values)) {
- $metadata['image'] = array_key_exists('og:image', $values) ? $values['og:image'] : $values['twitter:image'];
- } else {
- $metadata['image'] = null;
+ if (!$metadata['title']) {
+ if (array_key_exists('og:title', $values)) {
+ // Use facebook open graph title.
+ $metadata['title'] = $values['og:title'];
+ } elseif (array_key_exists('twitter:title', $values)) {
+ // Use twitter cards title.
+ $metadata['title'] = $values['twitter:title'];
+ }
}
+ if (array_key_exists('og:image', $values) || array_key_exists('twitter:image', $values)) {
+ $metadata['image'] = array_key_exists('og:image', $values) ? $values['og:image'] : $values['twitter:image'];
+ } else {
+ $metadata['image'] = null;
+ }
+
return $metadata;
}