从iframe视频获取youtube视频id


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

该字段包含ifrmae,类似于

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

从这里我想要这个"ISMzgunUono"。我搜索了论坛并点击了链接,但没有找到任何方法将我的"fileToUpload"字段保留在赛前。我关注的一些链接:从嵌入iframe代码中获取YouTube视频id而这个链接给出了一个错误如何在php或cakephp中从youtube的iframe获取视频id期待尽快回复:

严格来说,在PHP中,您应该能够运行:

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

并且它应该与ISMzgunUono相呼应。不过,这是未经测试的!如果有效,请告诉我。

使用javascript和正则表达式,您可以这样做:

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>

你可以试试这个:

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

预期输出应该是这样的:

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

顺便说一句,这是通过如何在php或cakehp中从youtube的iframe获取视频id获得的

最新更新