Strip YouTube Embed Code Down to URL Only

这一生的挚爱 提交于 2019-12-20 06:17:46

问题


Please help!

I need to strip the following code so that it only uses the "value" part

$<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>

So in this case it would strip it down to http://www.youtube.com/v/IkZuQ-aTIs0

The catch is that this is dynamic so it is pulling these embed codes for different files so they are changing.

Please help :D


回答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]) );

?>



回答2:


The best way is to use a DOM parser.

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>');



回答3:


$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;

little more complex than webartos, but will grab any value, instead of just the youtube link



来源:https://stackoverflow.com/questions/8536787/strip-youtube-embed-code-down-to-url-only

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