Does anyone know a regexp to extract just the video id from a Vimeo embed using php?

痴心易碎 提交于 2019-12-23 04:53:36

问题


Does anyone know a regexp to extract just the video id from a Vimeo <embed> tag using php?

eg:

13084859

from:

<object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=13084859&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=13084859&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object>

回答1:


Here:

$html = '<object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=13084859&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=13084859&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object>';

if (preg_match('/moogaloop\.swf\?clip_id=([0-9]+)/', $html, $matches)) {
  echo $matches[1];
} else {
  echo 'n/a';
}



回答2:


Here is a simple function I've written for you, is working fine, but its not using regex :)

<?php
function get_vimeo_id( $embed )
{
    $vimeo_id_array = explode( '?clip_id=', $embed );
    $vimeo_id_array_2 = explode( '&amp;', $vimeo_id_array[1] );
    $vimeo_id = $vimeo_id_array_2[0];

    return $vimeo_id;
}

// Get Vimeo Id
$vimeo_id = get_vimeo_id( '<object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=13084859&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=13084859&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object>' );

// Use $vimeo_id
echo $vimeo_id;
?>


来源:https://stackoverflow.com/questions/3214661/does-anyone-know-a-regexp-to-extract-just-the-video-id-from-a-vimeo-embed-using

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!