markdown PHP视频网址解析器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown PHP视频网址解析器相关的知识,希望对你有一定的参考价值。

<?php

/**
 * Video Url Parser
 *
 * Parses URLs from major cloud video providers. Capable of returning
 * keys from various video embed and link urls to manipulate and
 * access videos in various ways.
 */
class VideoUrlParser
{
    /**
     * Determines which cloud video provider is being used based on the passed url.
     *
     * @param string $url The url
     * @return null|string Null on failure to match, the service's name on success
     */
    public static function identify_service($url)
    {
        if (preg_match('%(?:https?:)?//(?:(?:www|m)\.)?(youtube(?:-nocookie)?\.com|youtu\.be)\/%i', $url)) {
            return 'youtube';
        } elseif (preg_match('%(?:https?:)?//(?:[a-z]+\.)*vimeo\.com\/%i', $url)) {
            return 'vimeo';
        }
        return null;
    }

    /**
     * Determines which cloud video provider is being used based on the passed url,
     * and extracts the video id from the url.
     *
     * @param string $url The url
     * @return null|string Null on failure, the video's id on success
     */
    public static function get_url_id($url)
    {
        $service = self::identify_service($url);

        if ($service == 'youtube') {
            return self::get_youtube_id($url);
        } elseif ($service == 'vimeo') {
            return self::get_vimeo_id($url);
        }
        return null;
    }

    /**
     * Determines which cloud video provider is being used based on the passed url,
     * extracts the video id from the url, and builds an embed url.
     *
     * @param string $url The url
     * @return null|string Null on failure, the video's embed url on success
     */
    public static function get_url_embed($url, $autoplay = 0)
    {
        $service = self::identify_service($url);

        $id = self::get_url_id($url);

        if ($service == 'youtube') {
            return self::get_youtube_embed($id, $autoplay);
        } elseif ($service == 'vimeo') {
            return self::get_vimeo_embed($id, $autoplay);
        }
        return null;
    }

    /**
     * Parses various youtube urls and returns video identifier.
     *
     * @param string $url The url
     * @return string the url's id
     */
    public static function get_youtube_id($url)
    {
        $youtube_url_keys = array('v', 'vi');

        // Try to get ID from url parameters
        $key_from_params = self::parse_url_for_params($url, $youtube_url_keys);
        if ($key_from_params) return $key_from_params;

        // Try to get ID from last portion of url
        return self::parse_url_for_last_element($url);
    }

    /**
     * Builds a Youtube embed url from a video id.
     *
     * @param string $youtube_video_id The video's id
     * @return string the embed url
     */
    public static function get_youtube_embed($youtube_video_id, $autoplay = 0)
    {
        $embed = "http://youtube.com/embed/$youtube_video_id?autoplay=$autoplay";

        return $embed;
    }

    /**
     * Parses various vimeo urls and returns video identifier.
     *
     * @param string $url The url
     * @return string The url's id
     */
    public static function get_vimeo_id($url)
    {
        // Try to get ID from last portion of url
        return self::parse_url_for_last_element($url);
    }

    /**
     * Builds a Vimeo embed url from a video id.
     *
     * @param string $vimeo_video_id The video's id
     * @return string the embed url
     */
    public static function get_vimeo_embed($vimeo_video_id, $autoplay = 1)
    {
        $embed = "http://player.vimeo.com/video/$vimeo_video_id?byline=0&amp;portrait=0&amp;autoplay=$autoplay";

        return $embed;
    }

    /**
     * Find the first matching parameter value in a url from the passed params array.
     *
     * @access private
     *
     * @param string $url The url
     * @param array $target_params Any parameter keys that may contain the id
     * @return null|string Null on failure to match a target param, the url's id on success
     */
    private static function parse_url_for_params($url, $target_params)
    {
        parse_str(parse_url($url, PHP_URL_QUERY), $my_array_of_params);
        foreach ($target_params as $target) {
            if (array_key_exists($target, $my_array_of_params)) {
                return $my_array_of_params[$target];
            }
        }
        return null;
    }

    /**
     * Find the last element in a url, without any trailing parameters
     *
     * @access private
     *
     * @param string $url The url
     * @return string The last element of the url
     */
    private static function parse_url_for_last_element($url)
    {
        $url_parts = explode("/", $url);
        $prospect = end($url_parts);
        $prospect_and_params = preg_split("/(\?|\=|\&)/", $prospect);
        if ($prospect_and_params) {
            return $prospect_and_params[0];
        } else {
            return $prospect;
        }
        return $url;
    }
}
Youtube/Vimeo Video Url Parser
==============================

Parses URLs from major cloud video providers. Capable of extracting keys from various video embed and link urls to manipulate and access videos in various ways.


Usage
-----
```php
VideoUrlParser::identify_service("https://www.youtube.com/watch?v=x_8kFbZf20I&feature=youtu.be");
// returns string "youtube"

VideoUrlParser::get_url_id("https://www.youtube.com/watch?v=x_8kFbZf20I&feature=youtu.be");
// returns string "x_8kFbZf20I"

VideoUrlParser::get_url_embed("https://www.youtube.com/watch?v=x_8kFbZf20I&feature=youtu.be");
// returns string "http://youtube.com/embed/x_8kFbZf20I?autoplay=1"
```


Supports
--------

- Vimeo
- Youtube

以上是关于markdown PHP视频网址解析器的主要内容,如果未能解决你的问题,请参考以下文章

你想要的资源都在这里,快点进去(PHP资源大全第五篇)

php 中的 Youtube 视频下载器失败 - 禁止

从网址下载SDCard上的视频

php怎么获得图片解析后的地址??

HyperDown.js 这个markdown解析器在浏览器中怎么使用

正则表达式之简易markdown文件解析器