如何从以下链接获取 youtube 网址?
<iframe title="YouTube video player" width="640" height="390"
src="http://www.youtube.com/embed/VvJ037b_kLs"
frameborder="0" allowfullscreen></iframe>
最佳答案
可以使用regex和preg_match函数
preg_match('/src="([^"]+)"/', $iframe_string, $match);
$url = $match[1];
更新 如果您有使用 php 生成的页面或作为 php 文件中的内联 html,您可以使用缓冲区从 php 中获取 html,然后在其上使用正则表达式:
首先在页面代码的开头使用 ob_start();
,或者如果您在 php 之前有一些 html,则可以通过添加将其用于整个页面:
<?php ob_start(); ?>
和 php 文件的开头,然后在最后,您可以在字符串中获取 ob 缓冲区并应用正则表达式:
<?php ob_start(); ?>
<iframe src="foo.bar"></iframe>
<iframe src="baz"></iframe>
<?php
$output = ob_get_contents();
ob_end_clean();
// find all iframes generated by php or that are in html
preg_match_all('/<iframe[^>]+src="([^"]+)"/', $output, $match);
$urls = $match[1];
echo $output;
?>
关于php - 如何使用php从iframe获取URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17017684/