get youtube video id from iframe video

ε祈祈猫儿з 提交于 2021-01-27 09:51:59

问题


<?php echo $videoFirstArray['fileToUpload']; ?>

This field contains the ifrmae,something like this

<iframe width="560" height="315" src="https://www.youtube.com/embed/ISMzgunUono" frameborder="0" allowfullscreen></iframe>

From this i want this "ISMzgunUono".I've searched the forum and follwed the link but didn't find the any way to keep my 'fileToUpload' field inside the pregmatch. Some of the links that i follwed: Get YouTube video id from embed iframe code and this link gives an error how to get Video id from iframe of youtube in php or cakephp Expecting replies soon:


回答1:


Strictly speaking, in PHP, you should be able to run:

preg_match('/youtube.com\/embed\/([A-Za-z0-9-]+)/', $videoFirstArray['fileToUpload#'], $matches);
if($matches[1]) {
    echo $matches[1];
}

And it should echo ISMzgunUono. This is untested, though! Let me know if it works.




回答2:


With javascript and regular expressions you can do like this:

var url = document.getElementById('myIframe').src,
    regExp = /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/,
    videoId = url.match(regExp);

if (videoId && videoId[1].length === 11) {
    console.log(videoId[1]);
}
<iframe id="myIframe" width="560" height="315" src="https://www.youtube.com/embed/ISMzgunUono" frameborder="0" allowfullscreen></iframe>



回答3:


You can try this:

<?php

$string = $videoFirstArray['fileToUpload'];
$pattern = '!youtube.com/embed/([^"]+)!i';
preg_match($pattern, $string, $match);
print_r($match);

?>

The expected output should be something like this :

Array
(
    [0] => youtube.com/embed/ISMzgunUono
    [1] => ISMzgunUono
)

Btw this was taken by how to get Video id from iframe of youtube in php or cakephp



来源:https://stackoverflow.com/questions/37522275/get-youtube-video-id-from-iframe-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!