Imdb\Title::trailers PHP Method

trailers() public method

Get the trailer URLs for a given movie
See also: IMDB page /trailers
Author: george
Author: izzy
public trailers ( boolean $full = FALSE, boolean $all = TRUE ) : mixed
$full boolean Retrieve all available data (TRUE), or stay compatible with previous IMDBPHP versions (FALSE, Default)
$all boolean Fetch all trailers (including off-site ones)? Default: True
return mixed trailers either array[0..n] of string ($full=FALSE), or array[0..n] of array[lang,title,url,restful_url,resolution] ($full=TRUE)
    public function trailers($full = FALSE, $all = TRUE)
    {
        if (empty($this->trailers)) {
            $page = $this->getPage("Trailers");
            if (empty($page)) {
                return array();
            }
            // no such page
            // due to site change, onsite / offsite trailers are mixed in on the same page
            // following code does not weed out offsite trailers.
            // Also $tag_s will be TRUE even if there are no trailers
            // old code -- $tag_s = strpos($this->page["Trailers"], '<div id="search-results">');
            $has_trailers = strpos($this->page["Trailers"], '<div id="search-results"><ol>');
            if ($has_trailers !== FALSE) {
                // if any on-site or off-site trailers exists
                $html_trailer = substr($this->page["Trailers"], $has_trailers, strpos($this->page["Trailers"], '</ol>', $has_trailers) - ($has_trailers + 1));
                // echo $html_trailer;
                // offsite trailer will have links like    href="/video/imdblink/vi.....
                if ($all) {
                    $regex = '@<a\\s*onclick=".*?"\\s*href="(/video/.*?/vi\\d+/)".*?><img.*?title="(.*?)"\\s*viconst=".*?"\\s*src="(.*?)"@s';
                } else {
                    $regex = '@<a\\s*onclick=".*?"\\s*href="(/video/(?!imdblink).*?/vi\\d+/)".*?><img.*?title="(.*?)"\\s*src="(.*?)"@s';
                }
                if (preg_match_all($regex, $html_trailer, $matches)) {
                    //print_r($matches);
                    for ($i = 0; $i < count($matches[0]); ++$i) {
                        $trailer = "http://" . $this->imdbsite . $matches[1][$i];
                        $res = strpos($matches[3][$i], 'HDIcon') !== FALSE ? 'HD' : 'SD';
                        if ($full) {
                            $this->trailers[] = array("lang" => '', "title" => html_entity_decode($matches[2][$i], ENT_QUOTES, 'UTF-8'), "url" => $trailer, "restful_url" => '', "resolution" => $res);
                        } else {
                            $this->trailers[] = $trailer;
                        }
                    }
                }
            }
        }
        return $this->trailers;
    }