Get YouTube video id from embed iframe code - php

I want to get the YouTube video ID from YouTube embed code using preg_match or regex. For a example
<iframe width="560" height="315" src="//www.youtube.com/embed/0gugBiEkLwU?rel=0" frameborder="0" allowfullscreen></iframe>
I want to take the ID 0gugBiEkLwU
Can anyone tell me how to do this. Really appropriate your help.

Using this pattern with a capturing group should give you the string you want:
d\/(\w+)\?rel=\d+"
example: https://regex101.com/r/kH5kA7/1

You can use :
src="\/\/(?:https?:\/\/)?.*\/(.*?)\?rel=\d*"
Check Demo Here
Explanation :

I know this is pretty late, but I came up with something for people who might still be looking.
Since not all Youtube iframe src attributes end in "?rel=", and can sometimes end in another query string or end with a double quote, you can use:
/embed\/([\w+\-+]+)[\"\?]/
This captures anything after "/embed/" and before the ending double-quote/query string. The selection can include any letter, number, underscore and hyphen.
Here's a demo with multiple examples: https://regex101.com/r/eW7rC1/1

The below function will extract the youtube video id from teh all format of youtube urls,
function getYoutubeVideoId($iframeCode) {
// Extract video url from embed code
return preg_replace_callback('/<iframe\s+.*?\s+src=(".*?").*?<\/iframe>/', function ($matches) {
// Remove quotes
$youtubeUrl = $matches[1];
$youtubeUrl = trim($youtubeUrl, '"');
$youtubeUrl = trim($youtubeUrl, "'");
// Extract id
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $youtubeUrl, $videoId);
return $youtubeVideoId = isset($videoId[1]) ? $videoId[1] : "";
}, $iframeCode);
}
$iframeCode = '<iframe width="560" height="315" src="http://www.youtube.com/embed/0gugBiEkLwU?rel=0" frameborder="0" allowfullscreen></iframe>';
// Returns youtube video id
echo getYoutubeVideoId($iframeCode);

Related

Regex Youtube - Set autoplay to 0 except for specific video hash

I want to remove all references to autoplay in an URL - even multiple times if they exist - for all videos except the one (Uj1ykZWtPYI). The other settings URL parameters should remain.
Source:
<iframe src="//www.youtube.com/embed/1337?wmode=transparent&autoplay=1&controls=0&showinfo=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe>
Desired:
<iframe src="//www.youtube.com/embed/1337?wmode=transparent&controls=0&showinfo=0&modestbranding=1&autoplay=0" frameborder="0" allowfullscreen=""></iframe>
It appends autoplay=0 programmatically.
For the specified video (Uj1ykZWtPYI), it should behave like this:
Source:
<iframe src="//www.youtube.com/embed/Uj1ykZWtPYI?wmode=transparent&autoplay=0&controls=0&showinfo=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe>
Desired:
<iframe src="//www.youtube.com/embed/Uj1ykZWtPYI?wmode=transparent&controls=0&showinfo=0&modestbranding=1&autoplay=1" frameborder="0" allowfullscreen=""></iframe>`
It appends autoplay=1 programmatically.
What I've tried so far in PHP:
// Non-matching specific video
$content['message'] = preg_replace('/youtube.com\/embed\/([^Uj1ykZWtPYI]*)([^"&]*)/', 'youtube.com/embed/$1$2&autoplay=0', $content['message']);
// Result
// <iframe src="//www.youtube.com/embed/W6hr-o6JiWs?wmode=transparent&autoplay=1&autoplay=0" frameborder="0" allowfullscreen="">
// Matching specific video
$content['message'] = preg_replace('/youtube.com\/embed\/([Uj1ykZWtPYI]*)([^"&]*)/', 'youtube.com/embed/$1$2&autoplay=1', $content['message']);
// Result
// <iframe src="//www.youtube.com/embed/Uj1ykZWtPYI?wmode=transparent&autoplay=1&autoplay=0&controls=0&showinfo=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe>
Matching all links without Uj1ykZWtPYI
You can search for this regular expression to find all matches without Uj1ykZWtPYI in the URL:
\b(src="(?:(?!Uj1ykZWtPYI|").)+?)(?:&autoplay=(?:1|0))([^"]*?)"|\b(src="(?:(?!Uj1ykZWtPYI|"|&autoplay=(?:1|0)).)+?)"
Then, replace the it with this (autoplay is zero):
$1$2$3&autoplay=0"
Explanation:
\b(src="(?:(?!Uj1ykZWtPYI|").)+?)(?:&autoplay=(?:1|0))([^"]*?)": The first part of the pattern looks for any characters after src=", which are not equal an apostrophe [^"] or !Uj1ykZWtPYI and stops at autoplay. This forms the first group. The pattern has to have the characters &autoplay=1 or &autoplay=0 in it. After autoplay, everything except the " character is included into the second group - until ".
\b(src="(?:(?!Uj1ykZWtPYI|"|&autoplay=(?:1|0)).)+?)": The second part matches any url without an autoplay, a " and Uj1ykZWtPYI in it, but otherwise is the same as the first pattern.
If pattern 1 matches, the groups $1 and $2 form the valid URL without autosave. If it does not match, but the second one does, $3 will contain the full URL. So, $1$2$3 depicts in any of the two cases the full URL. &autoplay=0 is then added to the full URL afterwards.
This pattern only works, if autoplay is not the first argument (?autoplay).
Matching all links including the video code Uj1ykZWtPYI
If you want to match every link with Uj1ykZWtPYI in it to add autoplay=1 you can use a pretty similar pattern:
\b(src="[^"]*?Uj1ykZWtPYI[^"]*?)(?:&autoplay=(?:1|0))([^"]*?)"|\b(src="[^"]*?Uj1ykZWtPYI(?:(?!&autoplay=(?:1|0))[^"])+?)"
Then replace it with this (autoplay is one):
$1$2$3&autoplay=1"
Live Example
Here you can see both patterns in action (JavaScript) to replace your example string (all four example string combinations are added):
// 1337 as code, including autoplay
var string1 = '<iframe src="//www.youtube.com/embed/1337?wmode=transparent&autoplay=1&controls=0&showinfo=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe>';
// Uj1ykZWtPYI as code, including autoplay
var string2 = '<iframe src="//www.youtube.com/embed/Uj1ykZWtPYI?wmode=transparent&autoplay=1&controls=0&showinfo=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe>';
// 1337 as code, autoplay not included
var string3 = '<iframe src="//www.youtube.com/embed/1337?wmode=transparent&controls=0&showinfo=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe>';
// Uj1ykZWtPYIas code, autoplay not included
var string4 = '<iframe src="//www.youtube.com/embed/Uj1ykZWtPYI?wmode=transparent&controls=0&showinfo=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe>';
var regex1 = /\b(src="(?:(?!Uj1ykZWtPYI|").)+?)(?:&autoplay=(?:1|0))([^"]*?)"|\b(src="(?:(?!Uj1ykZWtPYI|"|&autoplay=(?:1|0)).)+?)"/g;
var regex2 = /\b(src="[^"]*?Uj1ykZWtPYI[^"]*?)(?:&autoplay=(?:1|0))([^"]*?)"|\b(src="[^"]*?Uj1ykZWtPYI(?:(?!&autoplay=(?:1|0))[^"])+?)"/g;
var replacement1 = '$1$2$3&autoplay=0"';
var replacement2 = '$1$2$3&autoplay=1"';
console.log(string1.replace(regex1, replacement1));
console.log(string2.replace(regex2, replacement2));
console.log(string3.replace(regex1, replacement1));
console.log(string4.replace(regex2, replacement2));

Regex to remove iframe with facebook but keeps youtube

I want to remove only iframe(and everyhing inside iframe)with facebook like above but to keep youtube iframe:
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.example.com%2F%3Fp%313098&layout=standard&show_faces=true&width=500&action=recommend&colorscheme=light" ></iframe>
To keep iframes from youtube:
<iframe width="640" height="360" src="https://www.youtube.com/embed/hiYtWYLEjlI?rel=0" frameborder="0" allowfullscreen></iframe>
I've this regex but it only remove
<\/*i(?:frame|layer)|l(?:ayer|ink)[^>]*+>
https://regex101.com/r/eM9eS3/5
Better take the xpath approach:
$xml = simplexml_load_string($your_html_string);
$iframes = $xml->xpath("//iframe[contains(#src, 'facebook.com')]");
And delete these:
for ($i=0;$i<count($iframes);$i++) {
$iframe = $iframes[$i];
unset($iframe[0][0]);
}
Your new XML looks like:
echo $xml->asXML();
As whole function:
function goAwayFacebook($html) {
$xml = simplexml_load_string($html);
$iframes = $xml->xpath("//iframe[contains(#src, 'facebook.com')]");
for ($i=0;$i<count($iframes);$i++) {
$iframe = $iframes[$i];
unset($iframe[0][0]);
}
return $xml->asXML();
}
$newhtml = goAwayFacebook($html);
So you are roughly trying to check if www.facebook.com is present in <ifram> or not. This can be achieved by using following regex.
Regex: (?=.*www\.facebook\.com.*)<iframe .*<\/iframe>
Explanation:
(?=.*www\.facebook\.com.*) checks for presence of www.facebook.com between the <iframe> tags.
Regex101 Demo

How to convert youtube embed code to url?

There are a lot of examples of how to convert a youtube url to embed code, but I need a reverse code. I was never successful with all those expressions, so my question is How can I convert an embed code to a URL ? Thanks in advance.
In PHP, you can do that with DOMDocument to get the src attribute of the iframe, and preg_replace() to convert it to the video url:
$embed = '<html><head></head><body><p>Hello, there is the video <iframe width="560" height="315" src="http://www.youtube.com/embed/K75a2k_6QWs" frameborder="0" allowfullscreen></iframe> what do you think?</p></body></html>';
$doc = new DOMDocument();
$doc->loadHTML($embed);
while($iframe = $doc->getElementsByTagName('p')->item(0)->getElementsByTagName('iframe')->item(0)) {
$url = preg_replace(
'~/embed/~',
'/watch?v=',
$iframe->getAttribute('src')
);
$iframe->parentNode->replaceChild(
$doc->createTextNode($url),
$iframe
);
}
echo $result = $doc->getElementsByTagName('p')->item(0)->nodeValue; //'Hello, there is the video http://www.youtube.com/watch?v=K75a2k_6QWs what do you think?'

Pulling video thumbnail from TED embed code

I'm trying to pull out the video thumbnail from the TED video embed code. Why? Well, I'm using a WordPress theme that uses a custom field to handle video but the thumbnail function for that field isn't built for TED. I'm trying to re-jig it.
Here's the video thumbnail retrieval function (where YouTube and Vimeo are covered):
function woo_get_video_image($embed) {
$video_thumb = '';
/* Let's start by looking for YouTube, then Vimeo */
if ( preg_match( '/youtube/', $embed ) ) {
// YouTube - get the video code if this is an embed code (old embed)
preg_match( '/youtube\.com\/v\/([\w\-]+)/', $embed, $match);
// YouTube - if old embed returned an empty ID, try capuring the ID from the new iframe embed
if( !isset($match[1]) )
preg_match( '/youtube\.com\/embed\/([\w\-]+)/', $embed, $match);
// YouTube - if it is not an embed code, get the video code from the youtube URL
if( !isset($match[1]) )
preg_match( '/v\=(.+)&/',$embed ,$match);
// YouTube - get the corresponding thumbnail images
if( isset($match[1]) )
$video_thumb = "http://img.youtube.com/vi/".$match[1]."/0.jpg";
} else if ( preg_match( '/vimeo/', $embed ) ) {
// Vimeo - get the video thumbnail
preg_match( '#http://player.vimeo.com/video/([0-9]+)#s', $embed, $match );
if ( isset($match[1]) ) {
$video_id = $match[1];
// Try to get a thumbnail from Vimeo
$get_vimeo_thumb = unserialize(file_get_contents_curl('http://vimeo.com/api/v2/video/'. $video_id .'.php'));
$video_thumb = $get_vimeo_thumb[0]['thumbnail_large'];
}
}
// return whichever thumbnail image you would like to retrieve
return $video_thumb;
}
Here's a typical TED embed:
<iframe
src="http://embed.ted.com/talks/andy_puddicombe_all_it_takes_is_10_mindful_minutes.html"
width="560" height="315"
frameborder="0"
scrolling="no"
webkitAllowFullScreen mozallowfullscreen allowFullScreen>
</iframe>
And the TED API docs if that helps at all: http://developer.ted.com/API_Docs
I seem to be having trouble customizing the preg_match and/or $get_vimeo_thumb portions (at least that's what I think is going on). Basically, I'm learning this portion of PHP and it's bumpy.
you can try this
$source = 'http://www.ted.com/talks/andy_puddicombe_all_it_takes_is_10_mindful_minutes';
$tedJson = json_decode(file_get_contents('http://www.ted.com/talks/oembed.json?url='.urlencode($source)), TRUE);
pr($tedJson);
you will get the json in responce
I don't know what possessed me to answer this question, but here is a (tested working) quick and dirty. You'll probably want to throw some validation in there somewhere.. And if I were getting paid to do this it wouldn't be using file_get_contents and I'd probably use DOMDocument.
$embed = '<iframe
src="http://embed.ted.com/talks/andy_puddicombe_all_it_takes_is_10_mindful_minutes.html"
width="560" height="315"
frameborder="0"
scrolling="no"
webkitAllowFullScreen mozallowfullscreen allowFullScreen>
</iframe>';
function getThumbnail($embed){
preg_match("/src\=\"(.+)?\"/", $embed, $matches);
$uri = $matches[1];
preg_match("/posterUrl\s=\s\'(.+)?\'/", file_get_contents($uri), $matches);
echo $matches[1];
}
getThumbnail($embed);
We are taking the src of the iframe, getting the contents, and scrapping the JS embed variable to grab the image they use for the thumbnail.
Obviously you won't echo the output, and who knows if this is against their TOS. As a matter of fact I'd bet they at least wouldn't let you use this unless you kept the logo (which is not the case). Use at your own risk.

find image with specific src using preg_replace

I have some text with images within it. I want to replace specific images within the text with something else.
i.e. the text contains an a youtube img url that I want to replace with the actual video link.
<img class="mceItem" src="http://img.youtube.com/vi/1MsVzAkmds0/default.jpg" alt="1MsVzAkmds0">
and replace it with the youtube Iframe code:
<iframe title="'.$id.'" class="youtube-player" type="text/html" width="576" height="400" src="http://www.youtube.com/embed/'.$id.'" frameborder="0"></iframe>
my function looks like this:
function replacelink($link) {
$find= ("/<img src=[^>]+\>/i");
$replace = youtube("\\2");
return preg_replace($find,$replace);
}
What do I need to change in the regex to do the above?
Your regex is looking for <img src=, but there is a class attribute between img and src. Using $find= '/<img.*src=[^>]+>/i'; corrects the problem; however, this illustrates why you shouldn’t use regex to parse HTML.
You wrote:
I have some text with images within it.
If the text you’re referring to is actually HTML, then there are better alternatives to using regex for this.
Update
I believe this is what you’re looking for.
<?php
function replacelink($text) {
$replace = '<iframe title="$2" class="youtube-player" type="text/html" width="576" height="400" <iframe title="$2" class="youtube-player" type="text/html" width="576" height="400" src="http://www.youtube.com/embed/$2" frameborder="0"></iframe>';
$find = '/(<img.*?alt="([\da-z]+)".*?>)/i';
return preg_replace($find, $replace, $text);
}
$imagestr = '<img class="mceItem" src="http://img.youtube.com/vi/1MsVzAkmds0/default.jpg" alt="1MsVzAkmds0">';
echo replacelink($imagestr);
?>
There’s no need for a separate youtube() function.
If you want to replace more than one image, use preg_replace_all() instead of preg_replace().
The following regex would get all the images with a specific url. I not sure if this is what you wanted.
<img [^>]*?src="url"[^>]*?>
Previous anwser would fail if there were more than one image.

Categories