PHP code example of lorenzoferrarajr / lfj-dehydrator

1. Go to this page and download the library: Download lorenzoferrarajr/lfj-dehydrator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lorenzoferrarajr / lfj-dehydrator example snippets




use Lfj\Dehydrator\Dehydrator;
use Lfj\Dehydrator\Content\Content;
use Zend\Uri\Uri;

l->toString());
$content = new Content($html);

$dehydrator = new Dehydrator();

$dehydrator->addPlugin('MyDehydratorPlugin\YouTubeTitlePlugin');

$result = $dehydrator->dehydrate($url, $content)->getResult();

print_r($result);

array(
    'title' => array(
           0 => 'title from title tag',
           1 => 'title from og:title'
    )
)



namespace MyDehydratorPlugin;

use Lfj\Dehydrator\Plugin\AbstractPlugin;
use Lfj\Dehydrator\Plugin\PluginInterface;

class YouTubeTitlePlugin extends AbstractPlugin implements PluginInterface
{
    public function getKey()
    {
        return 'title';
    }

    public function run()
    {
        $xml = new \DOMDocument();
        $xml->loadHTML($this->getContent()->toString());

        $h1 = $xml->getElementById('watch-headline-title');

        $title = trim(str_replace("\n", '', $h1->nodeValue));

        $this->setResult($title);
    }

    public function isEnabled()
    {
        switch ($this->getUrl()->getHost())
        {
            case 'www.youtube.com':
            case 'youtube.com':
            case 'youtu.be':
                return true;
        }

        return false;
    }
}

ArrayIterator Object
(
    [storage:ArrayIterator:private] => Array
        (
            [title] => Array
                (
                    [0] => Chaplin Modern Times - Factory Scene (HD - 720p)
                    [1] => Chaplin Modern Times - Factory Scene (HD - 720p)
                )

        )
)

ArrayIterator Object
(
    [storage:ArrayIterator:private] => Array
        (
            [title] => Chaplin Modern Times - Factory Scene (HD - 720p)
        )

)