如何使用PHP从iframe获取url [英] How to Get url from the iframe using PHP
本文介绍了如何使用PHP从iframe获取url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从以下链接获取youtube网址?
How to get the youtube url from the below link?
<iframe title="YouTube video player" width="640" height="390"
src="http://www.youtube.com/embed/VvJ037b_kLs"
frameborder="0" allowfullscreen></iframe>
推荐答案
您可以使用正则表达式和preg_match函数
You can use regex and preg_match function
preg_match('/src="([^"]+)"/', $iframe_string, $match);
$url = $match[1];
UPDATE 如果在页面中生成页面html或php你可以使用缓冲区从php中获取html,然后在其上使用正则表达式:
UPDATE if have page generated in html or php you can use buffers to get html out of php and then use regex on it:
首先使用 ob_start();
和您的页面代码的开头,或者如果您在php之前有一些html,可以通过添加以下内容将其用于整个页面:
first use ob_start();
and the beginning of your page code or if you have some html before your php you can use it for whole page by adding:
<?php ob_start(); ?>
和php的开头文件,然后在结束时你可以获得字符串中的ob缓冲区并应用正则表达式:
and the beginning of the php file, then at the end you can get ob buffer in string and applying the regex:
<?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从iframe获取url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文