mirror of
https://github.com/j0k3r/php-readability.git
synced 2026-09-27 06:26:17 +00:00
dc590542f0cb8e14dbbef9841a0aca029f99b6d2
We append a new node when it isn't a `div` or `p` (like when it's an `article`) with the same id which generate a DOM error "DOMElement::setAttribute(): ID blabla already defined".
Readability
This is an extract of the Readability class from this full-text-rss fork. It can be defined as a better version of the original php-readability.
Differences
The default php-readability lib is really old and needs to be improved. I found a great fork of full-text-rss from @Dither which improve the Readability class.
- I've extracted the class from its fork to be able to use it out of the box
- I've added some simple tests
- and changed the CS, run
php-cs-fixerand added a namespace
But the code is still really hard to understand / read ...
Requirements
By default, this lib will use the Tidy extension if it's available. Tidy is only used to cleanup the given HTML and avoid problems with bad HTML structure, etc ..
Since Composer doesn't support suggestion on PHP extension, I write this suggestion here.
Usage
use Readability\Readability;
$url = 'http://www.medialens.org/index.php/alerts/alert-archive/alerts-2013/729-thatcher.html';
// you can use whatever you want to retrieve the html content (Guzzle, Buzz, cURL ...)
$html = file_get_contents($url);
$readability = new Readability($html, $url);
// or without Tidy
// $readability = new Readability($html, $url, 'libxml', false);
$result = $readability->init();
if ($result) {
// display the title of the page
echo $readability->getTitle()->textContent;
// display the *readability* content
echo $readability->getContent()->textContent;
} else {
echo 'Looks like we couldn\'t find the content. :(';
}
Languages
PHP
100%
