Php 仅将YouTube嵌入代码剥离到URL

Php 仅将YouTube嵌入代码剥离到URL,php,youtube,embed,strip,Php,Youtube,Embed,Strip,请帮忙 我需要剥离以下代码,以便它只使用“值”部分 $ 因此,在这种情况下,它将剥离到 问题是这是动态的,所以它会为不同的文件提取这些嵌入代码,所以它们会发生变化 请帮助:D最好的方法是使用DOM解析器 $doc=newDOMDocument(); $doc->loadHTML(“”); $string=''; $start=strpos($string,'value=“”); $string=substr($string,$start+7); $end=strpos($string,“”

请帮忙

我需要剥离以下代码,以便它只使用“值”部分

$
因此,在这种情况下,它将剥离到

问题是这是动态的,所以它会为不同的文件提取这些嵌入代码,所以它们会发生变化


请帮助:D

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

$doc=newDOMDocument();
$doc->loadHTML(“”);

$string='';
$start=strpos($string,'value=“”);
$string=substr($string,$start+7);
$end=strpos($string,“”);
$string=substr($string,0,$end);
echo$字符串;

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

谢谢大家的快速反馈!我想我需要更具体一点。。。基本上,这些数据是以XML格式存储的,我这样称呼它[代码]不要以这种方式解析HTML。当您看到一些损坏的HTML时会发生什么?让为这类事情构建的标准类来做吧,而不是重新发明轮子。我在DOMDocument方面也运气不好。。。。我不是那个图书馆的忠实粉丝VideoEmbed变量是存储YouTube嵌入代码的东西,我正试图去掉这些代码。如果这是我应该添加代码的地方,我如何添加您提供给本节的代码?很抱歉,我很难在这里正确显示代码。我是个笨蛋。我发了一篇新帖子,更好地解释了我的问题-@Brad,我知道。我不是解析HTML,只是提取URL。在这种情况下,DOMDocument可能有些过分。但如果是我,我会选择标准的解决方案,除非你能保证输入格式。DOMDocument无论如何都不是最快的解决方案,但是如果使用正确,您可以更可靠地遍历文档。
$<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>
$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>');
<?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]) );

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