user2466994
user2466994

Reputation: 329

How to Get url from the iframe using PHP

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> 

Upvotes: 23

Views: 45351

Answers (5)

jcubic
jcubic

Reputation: 66660

You can use regex and preg_match function

preg_match('/src="([^"]+)"/', $iframe_string, $match);
$url = $match[1];

UPDATE if you have page generated with php or as inline html in php file, you can use buffers to get html out of php and then use regex on it:

first use ob_start(); at 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(); ?>

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;

?>

Upvotes: 58

Deepa MG
Deepa MG

Reputation: 196

My html webcode is:

  <div url="doctor.videoLink"><div class="media-youtube-video">
          <iframe src="https://www.youtube.com/embed/YZvJZrfw5oo?rel=0&amp;enablejsapi=1" frameborder="0" allowfullscreen="" id="widget2"></iframe>
</div> 
</div>

And i retrive src from iframe as :

foreach($kolPro->find('div.video div div iframe') as $youTube)
                            {
                                //$kolsData['you_tube']=$youTube->innertext();
                                preg_match('/src="([^"]+)"/', $youTube, $match);
                                $youtubeLink= $match[1];
                            }

Upvotes: 0

ecabuk
ecabuk

Reputation: 1681

<?php
$url = '<iframe title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/VvJ037b_kLs" frameborder="0" allowfullscreen></iframe> ';
$tmpDoc = new DOMDocument();

# Dirty hack to support utf-8 with loadHTML
# http://php.net/manual/en/domdocument.loadhtml.php#95251
@$tmpDoc->loadHTML( '<?xml encoding="UTF-8">' . $url );
foreach ( $tmpDoc->childNodes as $item ) {
    if ( $item->nodeType == XML_PI_NODE ) {
        $tmpDoc->removeChild( $item );// remove hack
    }
}
$tmpDoc->encoding = 'UTF-8'; // insert proper

$body = $tmpDoc->getElementsByTagName( 'body' );

if ( ! $body->length ) {
    return null;
}

$nodes = $body->item( 0 )->childNodes;

if ( empty( $nodes ) ) {
    return null;
}

echo $nodes->item( 0 )->getAttribute( 'src' );

Upvotes: 0

Isaac Lim&#243;n
Isaac Lim&#243;n

Reputation: 2058

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 "";
}

Upvotes: 3

user3208692
user3208692

Reputation: 1

<?php
function getHtmlElement($html,$startOfElement,$endOfElement)
{
    $startingPosition = strpos($html, $startOfElement);
    $endPosition = strpos($html, $endOfElement,$startingPosition);
    $foundElement = substr($html, $startingPosition,$endPosition);

    return $foundElement;
}

    $html = file_get_contents('http://www.youtube.com/');
    $startOfElement = '<div id="guide"';
    $endOfElement = "<";

    $myElement = getHtmlElement($html,$startOfElement,$endOfElement);

    echo $myElement;

I was looking for a way to take html elements from url's but i didn't find one so i wrote this.It works just as shown in the example.After getting the element you can apply the same logic as the function i wrote to get a part of the string you want.

Upvotes: 0

Related Questions