如何使用 PHP 从 iframe 获取 url
如何从下面的链接获取youtube网址?
<iframe title="YouTube video player" width="640" height="390"
src="http://www.youtube.com/embed/VvJ037b_kLs"
frameborder="0" allowfullscreen></iframe>
如何从下面的链接获取youtube网址?
<iframe title="YouTube video player" width="640" height="390"
src="http://www.youtube.com/embed/VvJ037b_kLs"
frameborder="0" allowfullscreen></iframe>
您可以使用正则表达式和preg_match函数
preg_match('/src="([^"]+)"/', $iframe_string, $match);
$url = $match[1];
UPDATE 如果您有使用php生成的页面或在php文件中作为内联html生成的页面,则可以使用缓冲区从php中获取html,然后在其上使用正则表达式:
首先在页面代码的开头使用,或者如果您在php之前有一些html,则可以通过添加以下内容将其用于整个页面:ob_start();
<?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;
?>
public function makeiframevideo($url)
{
// Youtube
$ytRegExp = "/^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/";
preg_match($ytRegExp, $url, $ytMatch);
if ($ytMatch && strlen($ytMatch[1]) === 11) {
return "<iframe frameborder=\"0\" allowfullscreen class=\"pull-left\" id=\"ytplayer\" type=\"text/html\" width=\"100%\" height=\"405\" src=\"//www.youtube.com/embed/{$ytMatch[1]}?showinfo=0\" ></iframe>";
}
// Instagram
$igRegExp = "/^(?:https?:\/\/)?(?:www\.)?instagram.com\/p\/(.[a-zA-Z0-9\_]*)/";
preg_match($igRegExp, $url, $igMatch);
if ($igMatch && strlen($igMatch[0])) {
return "<iframe frameborder=\"0\" allowfullscreen class=\"pull-left\" id=\"ytplayer\" type=\"text/html\" width=\"100%\" height=\"405\" src='{$igMatch[0]}/embed/' ></iframe>";
}
// Vine
$vRegExp = "/^(?:https?:\/\/)?(?:www\.)?vine.co\/v\/(.[a-zA-Z0-9]*)/";
preg_match($vRegExp, $url, $vMatch);
if ($vMatch && strlen($vMatch[0])) {
return "<iframe frameborder=\"0\" allowfullscreen class=\"pull-left\" id=\"ytplayer\" type=\"text/html\" width=\"100%\" height=\"405\" src='{$vMatch[0]}/embed/simple' ></iframe>";
}
// Vimeo
$vimRegExp = " /\/\/(player.)?vimeo.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/";
preg_match($vimRegExp, $url, $vimMatch);
if ($vimMatch && strlen($vimMatch[3])) {
return "<iframe frameborder=\"0\" allowfullscreen class=\"pull-left\" id=\"ytplayer\" type=\"text/html\" width=\"100%\" height=\"405\" src='//player.vimeo.com/video/{$vimMatch[3]}' ></iframe>";
}
// Dailymotion
$dmRegExp = "/.+dailymotion.com\/(video|hub)\/([^_]+)[^#]*(#video=([^_&]+))?/";
preg_match($dmRegExp, $url, $dmMatch);
if ($dmMatch && strlen($dmMatch[2])) {
return "<iframe frameborder=\"0\" allowfullscreen class=\"pull-left\" id=\"ytplayer\" type=\"text/html\" width=\"100%\" height=\"405\" src='//www.dailymotion.com/embed/video/{$dmMatch[2]}' ></iframe>";
}
// Youku
$youkuRegExp = "/\/\/v\.youku\.com\/v_show\/id_(\w+)/";
preg_match($youkuRegExp, $url, $youkuMatch);
if ($youkuMatch && strlen($youkuMatch[1])) {
return "<iframe frameborder=\"0\" allowfullscreen class=\"pull-left\" id=\"ytplayer\" type=\"text/html\" width=\"100%\" height=\"405\" src='//player.youku.com/embed/{$youkuMatch[1]}' ></iframe>";
}
$mp4RegExp = '/^.+.(mp4|m4v)$/';
preg_match($mp4RegExp, $url, $mp4Match);
$oggRegExp = '/^.+.(ogg|ogv)$/';
preg_match($oggRegExp, $url, $oggMatch);
$webmRegExp = '/^.+.(webm)$/';
preg_match($webmRegExp, $url, $webmMatch);
if ($mp4Match || $oggMatch || $webmMatch) {
return $url;
}
return "";
}