php - 如何使用php从iframe获取URL

标签 php iframe

如何从以下链接获取 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/

相关文章:

php - 注入(inject)绑定(bind)到服务容器的实例

php - 关于网站结构的一些基本问题

javascript - Jquery管状加载多个实例

javascript - 如何使用同一子域上的 URL 访问 iframe 的元素?

javascript - 为什么 OneDrive js 选择器有时会出现 null opener

iframe - 为什么人们仍然使用 iframe?

php - 如何在php中用相应的多个字符替换多个字符?

php - Mongodb "Remote server has closed the connection"重启后第一次连接

php - 我如何重组此代码以使其仅显示一列的结果?

jquery - 全屏 Iframe