Find string inside a php tag

4

When using in several types of embed from several sites, and go get only the link. For example:

Youtube: <iframe width="420" height="315" src="https://www.youtube.com/embed/HLhuNbO0egU"frameborder="0" allowfullscreen></iframe>

Vimeo: <iframe src="https://player.vimeo.com/video/143592640?title=0&byline=0&portrait=0&badge=0"width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>

What I want is to just get the video link, that is, what's inside the src="". In the case of YouTube, I'll link

    
asked by anonymous 23.11.2015 / 11:11

2 answers

3

What you can do is preg_match of tag with a regular expression:

    $link_to_getURL= '<iframe width="420" height="315" src="https://www.youtube.com/embed/HLhuNbO0egU"frameborder="0" allowfullscreen></iframe>';
    $array = array();
    preg_match( '/src="([^"]*)"/i', $link_to_getURL, $array ) ;
    print_r($array[1]);    //link saida

The full link will exit at $array

    
23.11.2015 / 11:28
1

In JavaScript:

var arraySrc = new Array();
jQuery('iframe').each(function(){
    var src = jQuery(this).attr('src');
    arraySrc.push(src);
});

In PHP:

$strHtml = <<<EOD
<iframe width="420" height="315" src="https://www.youtube.com/embed/HLhuNbO0egU"frameborder="0" allowfullscreen></iframe>
<iframe src="https://player.vimeo.com/video/143592640?title=0&byline=0&portrait=0&badge=0"width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
EOD;

preg_match_all('~<(iframe).*src="([^"]+)".*></>~', $strHtml, $match);

$arraySrc = array();
foreach($match[2] as $k => $value){
    $arraySrc[] = $value;
}
    
23.11.2015 / 11:38