tacman/php-readability

Automatic article extraction from HTML, fork of j0k3r/php-readability

Maintainers

Package info

github.com/tacman/php-readability

pkg:composer/tacman/php-readability

Transparency log

Fund package maintenance!

j0k3r

Statistics

Installs: 58

Dependents: 1

Suggesters: 0

Stars: 0

2.1.0 2026-08-06 18:29 UTC

README

CI Coverage Status Total Downloads License

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-fixer and added a namespace

But the code is still really hard to understand / read ...

This fork (tacman/php-readability)

This is tacman's fork, kept current for PHP 8.5 / Symfony 8 consumers. The tac branch is periodically merged forward from upstream/master (j0k3r/php-readability) to stay close to upstream. tacman/graby (also a tac-branch fork) depends on this package by name, so pulling in that graby fork brings this one in transitively — you don't need to require it directly unless you use Readability standalone.

To use it directly in a consuming app, point composer.json at the tac branch — no repositories block needed, Composer resolves it straight from Packagist:

composer require tacman/php-readability:dev-tac

Tagged releases (e.g. 2.1.0) are also published and satisfy a plain "*" constraint if you'd rather float to the latest stable tag instead of tracking the branch.

Requirements

By default, this lib will use the Tidy extension if it's available (checked via PHP's function_exists('tidy_parse_string'), no hard ext-tidy requirement). Tidy is only used to cleanup the given HTML and avoid problems with bad HTML structure, etc .. It'll be suggested by Composer, not required — the lib works fine without it installed, just with lower extraction quality on badly-formed HTML.

Also, if you got problem from parsing a content without Tidy installed, please install it and try again.

Usage

Note Call ->init() exactly once, explicitly, after constructing a Readability instance — the constructor does not call it for you. Calling it twice re-runs extraction against an already-consumed DOM and silently produces empty results on the second pass.

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. :(';
}

If you want to debug it, or check what's going on, you can inject a logger (which must follow Psr\Log\LoggerInterface, Monolog for example):

use Readability\Readability;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$url = 'http://www.medialens.org/index.php/alerts/alert-archive/alerts-2013/729-thatcher.html';
$html = file_get_contents($url);

$logger = new Logger('readability');
$logger->pushHandler(new StreamHandler('path/to/your.log', Logger::DEBUG));

$readability = new Readability($html, $url);
$readability->setLogger($logger);