Saturday, 15 August 2015

php - How to get the id from iframe? -



php - How to get the id from iframe? -

i tring youtube id iframe. , using link regular look given link here

i using code getting array(0){} result. here code:

`

preg_match('/src="\/\/(?:https?:\/\/)?.*\/(.*?)\?rel=\d*"/',$urldd, $matches); var_dump($matches); echo $matches[0][0];`

i poor in using preg_match please can help me youtube id iframe properly.

alternatively, can utilize domdocument particular url, utilize parse_url(). sample code:

$html = '<iframe width="560" height="315" src="//www.youtube.com/embed/0gugbieklwu?rel=0" frameborder="0" allowfullscreen></iframe>'; $dom = new domdocument(); $dom->loadhtml($html); $iframe = $dom->getelementsbytagname('iframe'); $url = ''; foreach($iframe $tag) { $url = $tag->getattribute('src'); } $output = parse_url($url, php_url_path); $pieces = array_filter(explode('/', $output)); $id = end($pieces); // should output 0gugbieklwu

if insist utilize regex, this:

$pattern = '#(?<=(?:v|i)=)[a-za-z0-9-]+(?=&)|(?<=(?:v|i)\/)[^&\n]+|(?<=embed\/)[^"&\n]+|(?<=‌​(?:v|i)=)[^&\n]+|(?<=youtu.be\/)[^&\n]+#'; $html = '<iframe width="560" height="315" src="//www.youtube.com/embed/0gugbieklwu?rel=0" frameborder="0" allowfullscreen></iframe>'; preg_match($pattern, $html, $matches); $url = reset($matches); $url = strtok($url, '?'); echo $url; // 0gugbieklwu

php iframe youtube

No comments:

Post a Comment