Get youtube video id from youtube url in smarty template?

vignesh

I need to parse and get youtube video id from youtube url inside smarty tpl file. Right now I'm using plugin file to achieve this using php preg_match function. But I want to use Smarty regular expressions to parse instead of the above method.

As I'm getting video URL through smarty loop, I couldn't parse it in php and assign in smarty. Currently I'm using this code to get the video id from PHP plugin file of smarty.

if ($videoURL) {            
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $videoURL, $match)) {
     $video_id = $match[1];
    }

     if (false !== $video_id) {
       return $video_id;
     }
     return false;
}

I'm not sure of how to convert this preg_match to smarty. Can anyone help me out of this problem?

Kieran

Your best bet is going to be extending Smarty with a plugin, specifically a modifier.

You can follow this discussion here to figure out the best method for getting the ID from a youtube URL; it is in fact quite tricky to account for the different types of URL using solely regex (hence the huge expression you have above.)

An example would look something like:

<?php 

function smarty_modifier_youtubeID($string) { 
    parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
    return $my_array_of_vars['v']; 
}

Then drop the above in the plugins/ directory of smarty with a name like modifier.youtube.php. Then proceed to use it in your for loop (within the template file) like so {$value|youtubeID}

The alternative, that I get the feeling you're after, is to do something like so:

{$value|regex_replace:"/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/":'$1'} 

The above will replace the URL with the video ID (assuming $value consists solely of a youtube URL and no other data, otherwise you'll have to modify the expression.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I get the YouTube video ID from a URL?

How to get youtube video id from URL with java?

Regex to extract both video id or playlist id from youtube url

How to Get Youtube Video URL Programmatically

Extract the video ID from youtube url in .net

Youtube Video Id from URL - Swift3

How can I get direct video url from YouTube video URL?

How can I get a Youtube video ID from the Youtube API given the video url?

How to get thumbnail of youtube video from url using php and API

php Youtube Get video id from live_stream

How to get direct url of video from youtube url?

Access Youtube Video URL or ID in MediaWiki API

Flutter Dart : How to extract youtube video ID from video URL?

How to get live video id from YouTube channel

GET YouTube Video URL - Data API 3.0

Get all video ID's from a YouTube playlist into a PHP array

Fetch YouTube Video Id from youtube URL in zend framework

separate get requests for each youtube video ID

How to get video tags from youtube video?

Access YouTube Video URL With YouTube Video ID

Youtube video id (or URL) to "user" and "channel"

Embed Youtube video from URL

Get youtube video Id from shared video Url

how to get youtube player id and stop video

How to get ID of last uploaded video to YouTube?

Is it possible to get url of first YouTube video from search?

How do I parse YouTube URLs to get the video ID from a URL using Dart?

I need some help in correcting my codes in php - How to get the video id from a YouTube URL

Is there solution "get youtube video ID from url" contains mobile web url?