src/Extractor/ContentExtractor.php
private function extractJsonLdInformation($html)
    {
        preg_match_all('/<script type\=\"application\/ld\+json\"\>([\s\S]*?)<\/script>/i', $html, $matches);

        if (!isset($matches[1])) {
            return;
        }

        $ignoreNames = [];
        $candidateNames = [];

        foreach ($matches[1] as $matche) {
            $data = json_decode(trim($matche), true);

            if (isset($data['@type']) && \in_array($data['@type'], ['Organization', 'WebSite', 'Person'], true)) {
                if (isset($data['name'])) {
                    $ignoreNames[] = $data['name'];
                }
                continue;
            }

            $this->logger->info('JSON-LD data: {JsonLdData}', ['JsonLdData' => $data]);

            // just in case datePublished isn't defined, we use the modified one at first
            if (isset($data['dateModified'])) {
                $this->date = $data['dateModified'];
            }

            if (isset($data['datePublished'])) {
                $this->date = $data['datePublished'];
            }

            // sometimes the date is an array
            if (\is_array($this->date)) {
                $this->date = reset($this->date);
            }

            // body should be a DOMNode
            if (isset($data['articlebody'])) {
                $dom = new \DOMDocument('1.0', 'utf-8');
                $this->body = $dom->createElement('p', htmlspecialchars(trim($data['articlebody'])));
            }

            if (isset($data['headline'])) {
                $candidateNames[] = $data['headline'];
            }

            if (isset($data['name'])) {
                $candidateNames[] = $data['name'];
            }

            if (isset($data['author']['name'])) {
                $authors = $data['author']['name'];

                if (false === \is_array($authors)) {
                    $authors = [$authors];
                }

                foreach ($authors as $author) {
                    $this->addAuthor($author);
                }
            }
        }

        if (\is_array($candidateNames) && \count($candidateNames) > 0) {
            foreach ($candidateNames as $name) {
                if (!\in_array($name, $ignoreNames, true)) {
                    $this->title = $name;
                }
            }
        }
    }
private function extractJsonLdInformation($html)
    {
        preg_match_all('/<script type\=\"application\/ld\+json\"\>([\s\S]*?)<\/script>/i', $html, $matches);

        if (!isset($matches[1])) {
            return;
        }

        $ignoreNames = [];
        $candidateNames = [];

        foreach ($matches[1] as $matche) {
            $data = json_decode(trim($matche), true);

            if (isset($data['@type']) && \in_array($data['@type'], ['Organization', 'WebSite', 'Person'], true)) {
                if (isset($data['name'])) {
                    $ignoreNames[] = $data['name'];
                }
                continue;
            }

            $this->logger->info('JSON-LD data: {JsonLdData}', ['JsonLdData' => $data]);

            // just in case datePublished isn't defined, we use the modified one at first
            if (isset($data['dateModified'])) {
                $this->date = $data['dateModified'];
            }

            if (isset($data['datePublished'])) {
                $this->date = $data['datePublished'];
            }

            // sometimes the date is an array
            if (\is_array($this->date)) {
                $this->date = reset($this->date);
            }

            // body should be a DOMNode
            if (isset($data['articlebody'])) {
                $dom = new \DOMDocument('1.0', 'utf-8');
                $this->body = $dom->createElement('p', htmlspecialchars(trim($data['articlebody'])));
            }

            if (isset($data['headline'])) {
                $candidateNames[] = $data['headline'];
            }

            if (isset($data['name'])) {
                $candidateNames[] = $data['name'];
            }

            if (isset($data['author']['name'])) {
                $authors = $data['author']['name'];

                if (false === \is_array($authors)) {
                    $authors = [$authors];
                }

                foreach ($authors as $author) {
                    $this->addAuthor($author);
                }
            }
        }

        if (\is_array($candidateNames) && \count($candidateNames) > 0) {
            foreach ($candidateNames as $name) {
                if (!\in_array($name, $ignoreNames, true)) {
                    $this->title = $name;
                }
            }
        }
    }
private function extractJsonLdInformation ( $ html ) { preg_match_all ( '/<script type\=\"application\/ld\+json\"\>([\s\S]*?)<\/script>/i' , $ html , $ matches ) ; if ( ! isset ( $ matches [ 1 ] ) ) { return ; } $ ignoreNames = [ ] ; $ candidateNames = [ ] ; foreach ( $ matches [ 1 ] as $ matche ) { $ data = json_decode ( trim ( $ matche ) , true ) ; if ( isset ( $ data [ '@type' ] ) && \ in_array ( $ data [ '@type' ] , [ 'Organization' , 'WebSite' , 'Person' ] , true ) ) { if ( isset ( $ data [ 'name' ] ) ) { $ ignoreNames [ ] = $ data [ 'name' ] ; } continue ; } $ this -> logger -> info ( 'JSON-LD data: {JsonLdData}' , [ 'JsonLdData' => $ data ] ) ; // just in case datePublished isn't defined, we use the modified one at first if ( isset ( $ data [ 'dateModified' ] ) ) { $ this -> date = $ data [ 'dateModified' ] ; } if ( isset ( $ data [ 'datePublished' ] ) ) { $ this -> date = $ data [ 'datePublished' ] ; } // sometimes the date is an array if ( \ is_array ( $ this -> date ) ) { $ this -> date = reset ( $ this -> date ) ; } // body should be a DOMNode if ( isset ( $ data [ 'articlebody' ] ) ) { $ dom = new \ DOMDocument ( '1.0' , 'utf-8' ) ; $ this -> body = $ dom -> createElement ( 'p' , htmlspecialchars ( trim ( $ data [ 'articlebody' ] ) ) ) ; } if ( isset ( $ data [ 'headline' ] ) ) { $ candidateNames [ ] = $ data [ 'headline' ] ; } if ( isset ( $ data [ 'name' ] ) ) { $ candidateNames [ ] = $ data [ 'name' ] ; } if ( isset ( $ data [ 'author' ] [ 'name' ] ) ) { $ authors = $ data [ 'author' ] [ 'name' ] ; if ( false === \ is_array ( $ authors ) ) { $ authors = [ $ authors ] ; } foreach ( $ authors as $ author ) { $ this -> addAuthor ( $ author ) ; } } } if ( \ is_array ( $ candidateNames ) && \ count ( $ candidateNames ) > 0 ) { foreach ( $ candidateNames as $ name ) { if ( ! \ in_array ( $ name , $ ignoreNames , true ) ) { $ this -> title = $ name ; } } } }
Extract data from JSON-LD information.

@param string $html Full page HTML

@see https://json-ld.org/spec/latest/json-ld/
Extract data from JSON - LD information .