Find string inside a php tag

Asked

Viewed 545 times

4

When using in various types of embed from various websites, go only search for 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 stay only with the video link, IE, what is inside src="". In the case of youtube I get https://www.youtube.com/embed/HLhuNbO0egU

  • 1

    Related : http://answall.com/questions/87372/php-regex-para-obten-2-groups-de-link-href

  • Are you doing it right in the browsed or is it a capture of SOAP, CURL php?

  • within the browser, embed comes from the database

2 answers

3


What you can do is a 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 be on $array

1

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="([^"]+)".*></\1>~', $strHtml, $match);

$arraySrc = array();
foreach($match[2] as $k => $value){
    $arraySrc[] = $value;
}
  • What is this <<<EOD?

  • @Diegofelipe http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

  • Interesting, I didn’t know this feature yet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.