剥离YouTube仅将代码嵌入URL

时间:2011-12-16 16:04:03

标签: php youtube embed strip

请帮忙!

我需要删除以下代码,以便它只使用"值"部分

$<object width="360" height="226"><param name="movie" value="http://www.youtube.com/v/IkZuQ-aTIs0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/IkZuQ-aTIs0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="360" height="226"></embed></object>

因此,在这种情况下,它会将其剥离为http://www.youtube.com/v/IkZuQ-aTIs0

问题在于这是动态的,因此它会为不同的文件提取这些嵌入代码,以便它们正在发生变化。

请帮助:D

3 个答案:

答案 0 :(得分:1)

<?php

$string = '<object width="360" height="226"><param name="movie" value="http://www.youtube.com/v/IkZuQ-aTIs0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/IkZuQ-aTIs0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="360" height="226"></embed></object>';

preg_match_all('#http://www.youtube.com/v/([\w\-]+){11}#is', $string, $matches);

print_r( array_unique($matches[0]) );

?>

答案 1 :(得分:0)

最好的方法是使用DOM解析器。

http://php.net/manual/en/class.domdocument.php

$doc = new DOMDocument();
$doc->loadHTML('<object width="360" height="226"><param name="movie" value="http://www.youtube.com/v/IkZuQ-aTIs0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/IkZuQ-aTIs0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="360" height="226"></embed></object>');

答案 2 :(得分:0)

$string = '<object width="360" height="226"><param name="movie" value="http://www.youtube.com/v/IkZuQ-aTIs0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/IkZuQ-aTIs0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="360" height="226"></embed></object>';

$start = strpos($string, 'value="');
$string = substr($string, $start + 7);
$end = strpos($string, '" ');
$string = substr($string, 0, $end);

echo $string;

比webartos稍微复杂,但会抓取任何值,而不仅仅是youtube链接