aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/poche/PocheReadability.php
blob: 48ae90d0341d0e8668006b8c2df1c5443025190f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

class PocheReadability extends Readability
{
    /**
    * Get the article title as an H1.
    *
    * @return DOMElement
    */
    protected function getArticleTitle() {
        $curTitle = '';
        $origTitle = '';

        try {
            $curTitle = $origTitle = $this->getInnerText($this->dom->getElementsByTagName('title')->item(0));
        } catch(Exception $e) {}
        
        if (preg_match('/ [\|\-] /', $curTitle))
        {
            $curTitle = preg_replace('/(.*)[\|\-] .*/i', '$1', $origTitle);
            
            if (count(explode(' ', $curTitle)) < 3) {
                $curTitle = preg_replace('/[^\|\-]*[\|\-](.*)/i', '$1', $origTitle);
            }
        }
        else if(strlen($curTitle) > 150 || strlen($curTitle) < 15)
        {
            $hOnes = $this->dom->getElementsByTagName('h1');
            if($hOnes->length == 1)
            {
                $curTitle = $this->getInnerText($hOnes->item(0));
            }
        }

        $curTitle = trim($curTitle);

        if (count(explode(' ', $curTitle)) <= 4) {
            $curTitle = $origTitle;
        }
        
        $articleTitle = $this->dom->createElement('h1');
        $articleTitle->innerHTML = $curTitle;
        
        return $articleTitle;
    }
}