2011-08-05 134 views
0

這是我一直在辛苦開發的插件的核心。雖然我遇到了一些麻煩與模式..PHP幫助preg_match模式

<?php 

$s = ' 
<embed type="application/x-shockwave-flash" id="single2" name="single2" src="http://api.realitylapse.com/player.swf" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" flashvars="file=http://cerium.realitylapse.com/stream/bea352a230ebd36b52dc27d874f41f5a/4e3c5eca/default/xxxxx/xxxxx-lq.mp4&amp;plugins=ltas&amp;ltas.cc=inhldvymihzxqln&amp;provider=http" height="424" width="659"> 

<embed type="application/x-shockwave-flash" src="http://www.xxxxx.com/player9397/player.swf?" quality="high" allowfullscreen="true" allowscriptaccess="always" wmode="opaque" flashvars="provider=http&amp;file=http://www.xxxxx.com/player9397/vb.php?id=TT175YivmF4y&amp;type=video&amp;backcolor=111111&amp;frontcolor=cccccc&amp;lightcolor=DE4949&amp;stretching=fill" height="420" width="99%"> 

<embed src="http://www.megavideo.com/v/P5X0UOA267fb79acd04cdb29a057c3fa0066573a1" type="application/x-shockwave-flash" allowfullscreen="true" width="100%" height="438">  
'; 


$patterns = array(); 
$patterns[] = '<embed[^>]+src=["\'](.+?)["\']'; 
$patterns[] = '<embed[^>]+data=["\'](.+?)["\']'; 
$patterns[] = '<embed[^>]+flashvars="(.+?)["\']'; //Possible problem.. 
$patterns[] = '<embed[^>]+file=(.+?)[&]';   // 
$patterns[] = '<iframe[^>]+src=["\'](.+?)["\']'; 
$patterns[] = '<iframe[^>]+data=["\'](.+?)["\']'; 
$patterns[] = '<object[^>]+src=["\'](.+?)["\']'; 
$patterns[] = '<object[^>]+data=["\'](.+?)["\']'; 
$patterns[] = '<video[^>]+src=["\'](.+?)["\']'; 
$patterns[] = '<video[^>]+data=["\'](.+?)["\']'; 
$patterns[] = '<video[^>]+file=(.+?)[&]'; 



$patterns = "#(?:" . implode("|", $patterns) . ")#si"; 

preg_match_all($patterns, ($s), $m); 
if (!empty($m[0])) 
{ 
    $edata = array(); 
    foreach($m[0] as $match) 
    { 

//Embeds: 
if (preg_match('#realitylapse.com/stream/(.+?)[&,"\']#si', $match, $id)) 
    $edata[] = "<!--nextpage--><!--tab_title:CERIUM-->\n[cerium " . $id[1] . "]"; 

else if (preg_match('#http&amp;file=http://www.xxxx.com/player9397/vb.php?id=(.+?)[&,"\']#si', $match, $id)) 
    $edata[] = "<!--nextpage--><!--tab_title:UNKNOWN-->\n[vb " . $id[1] . "]"; 

else if (preg_match('#http://www.megavideo.com/v/(.+)[&"\']#si', $match, $id)) 
    $edata[] = "<!--nextpage--><!--tab_title:MEGAVIDEO-->\n[megavideo " . $id[1] . "]"; 

    } 

if (isset($edata[0])) { 

$embeds = implode("\n", ($edata)); 

print $embeds; 

} 
    } 

?> 

這隻輸出:

[megavideo P5X0UOA267fb79acd04cdb29a057c3fa0066573a1] 

其它玩家嵌入我的比賽。任何在flashvars區域中的東西都不會。這可能不是真正的原因。 ..有像megavideo嵌入式的東西。我感謝任何幫助,謝謝!

回答

3

而不是使用正則表達式的,我會建議使用HTML解析器,如: http://simplehtmldom.sourceforge.net/

然後,您可以輕鬆地閱讀的元素屬性。

$html = str_get_html('<embed id="single2" height="424" width="659" flashvars="file=http://cerium.realitylapse.com/stream/bea352a230e" >'); 

$embed = $html->find('embed', 1); 

$embed->height; // == "424" 
$embed->flashvars; // == "file=http://cerium.realitylapse.com/stream/bea352a230e" 
+0

例如[DOMDocument](http://php.net/manual/en/class.domdocument.php)是默認情況下在PHP上啓用的HTML解析器。 –

+0

..是的,我一次又一次地聽到這個消息。用regexp去處理它可能會更好,但現在有點太晚了。我已經對這些模式有了相當的瞭解,所以它不再是一個問題。儘管如此,有一點讓分析器很適合它。 – Suffice