如何使用 PHP 从 iframe 获取 url

2024-05-12

如何从下面的链接获取 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];

UPDATE如果您有使用 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;

?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 PHP 从 iframe 获取 url 的相关文章

随机推荐