我有一组youtube iframes / objects如下:
[0] => <iframe width="600" height="338" src="http://www.youtube.com/embed/szL_PVuzWp0?fs=1&feature=oembed" frameborder="0" allowfullscreen></iframe>
[1] => <object width="600" height="338"><param name="movie" value="http://www.youtube.com/v/jm1S43a-e3Y?version=3&feature=oembed"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/jm1S43a-e3Y?version=3&feature=oembed" type="application/x-shockwave-flash" width="600" height="338" allowscriptaccess="always" allowfullscreen="true"></embed></object>
[2] => <iframe width="600" height="338" src="http://www.youtube.com/embed/7fTploFSbXA?fs=1&feature=oembed" frameborder="0" allowfullscreen></iframe>
[3] => <iframe width="600" height="338" src="http://www.youtube.com/embed/vQSRNYgiuMk?fs=1&feature=oembed" frameborder="0" allowfullscreen></iframe>
请注意,嵌入方法可能会有所不同(通常<iframe>
,偶尔会<object>
)(由于外部数据源)。
我如何/为每个人提取视频网址(例如vQSRNYgiuMk或jm1S43a-e3Y)的最可靠方法是什么?
最终我希望得到一个像这样的数组:
[0] => "szL_PVuzWp0"
[1] => "jm1S43a-e3Y"
[2] => "7fTploFSbXA"
[3] => "vQSRNYgiuMk"
答案 0 :(得分:5)
请请使用正则表达式:
$dom_document = new DOMDocument();
$dom_document->loadHTML($html);
//use DOMXpath to navigate the html with the DOM
$dom_xpath = new DOMXpath($dom_document);
// if you want to get the all the iframes
$iframes = $dom_xpath->query("//iframe");
if (!is_null($iframes)) {
foreach ($iframes as $iframe) {
if($iframe->hasAttributes()){
$attributes = $iframe->attributes;
if(!is_null($attributes)){
foreach ($attributes as $index=>$attr){
if($attr->name == 'src'){
$curSrc = $attr->value;
//use regex here to extract what you want
}
}
}
}
}
}
不完整的解决方案。但是你明白了......
答案 1 :(得分:0)
foreach($arr as $i=>$a){
$start = strpos($a, "/v/") + 3;
if(!$start) $start = strpos($a, "/embed/") + 7;
$qm = strpos("?");
$length = $qm - $start;
$new_array[$i] = substr($a, $start, $length);
}