如何使用PHP从iframe获取url

use*_*994 19 php iframe

如何从以下链接获取youtube网址?

<iframe title="YouTube video player" width="640" height="390" 
 src="http://www.youtube.com/embed/VvJ037b_kLs" 
frameborder="0" allowfullscreen></iframe> 
Run Code Online (Sandbox Code Playgroud)

jcu*_*bic 41

您可以使用regex和preg_match函数

preg_match('/src="([^"]+)"/', $iframe_string, $match);
$url = $match[1];
Run Code Online (Sandbox Code Playgroud)

更新如果您使用php生成的页面或php文件中的内联html,您可以使用缓冲区从PHP获取html然后使用正则表达式:

首先ob_start();在页面代码的开头使用,或者如果你在php之前有一些html,你可以通过添加以下内容将它用于整个页面:

<?php ob_start(); ?>
Run Code Online (Sandbox Code Playgroud)

和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;

?>
Run Code Online (Sandbox Code Playgroud)