2010-08-17 32 views
0

我有我已經寫,以便提取一個用戶輸入的值和替換一些高度和寬度值和保持URL的正則表達式。這是因爲它可以安全地添加到數據庫中。嵌入代碼的正則表達式將不起作用

這是我迄今爲止(只是想獲得的preg_match返回真值)

$test ='<object height="81" width="100%"> <param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Ftheshiverman%2Fsummer-beats-july-2010&secret_url=false"></param> <param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Ftheshiverman%2Fsummer-beats-july-2010&secret_url=false" type="application/x-shockwave-flash" width="100%"></embed> </object>'; 
    if (preg_match('/<object height=\"[0-9]*\" width=\"[0-9]*\"><param name=\"movie\" value=\"(.*)\"><\/param><param name=\"allowscriptaccess\" value=\"always\"><\/param><embed allowscriptaccess=\"always\" height=\"[0-9]*\" src=\".*\" type=\"application\/x-shockwave-flash\" width=\"100%\"><\/embed><\/object>/', $test)) { 

$embed = $test; 

} else { 

$embed = 'FALSE'; 

} 

我好像做錯了驗證,因爲它總是返回false。

+0

解析(X)HTML用正則表達式是[危險的事情。(http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-自包含的標籤/ 1732454#1732454) – NullUserException 2010-08-17 06:07:58

回答

0

如果你想要做的是允許用戶給你SoundCloud嵌入,而你保留玩家風格的權利,你可能需要考慮SoundCloud(see here)和其他各方很好支持的oEmbed。通過這種方式,用戶只需輸入自己的正常軌道的網址,你可以解決那些在後臺,你認爲合適。

而且,記住順序不同的<param>的嵌入代碼仍然是一個有效的嵌入代碼,但將是非常困難的,以配合正則表達式

1

我看到失敗的第一件事是:

width="100%" will not match /width=\"[0-9]*\"/ 

我不知道正則表達式的確切定義PHP;但我不知道這將匹配(在REG-表達的空間可以匹配目標文本零個或多個空格,但周圍的其他方法將無法正常工作):

> <param  will not match (probably) /><param/ 

正如你可以看到解析XML與正則表達式很難且容易出錯。
你真正想要做的是使用XML SAX解析器。

試試這個:PS我的PHP也不是很大,因此它可以包含錯誤。

PS。 XML的長URL沒有正確編碼。我在這裏使用urlencode()來停止錯誤信息。我沒有檢查是否有意義。

<?php 

$test = '<object height="81" width="100%">' 
      .'<param name="movie" value="' 
       .urlencode('http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Ftheshiverman%2Fsummer-beats-july-2010&secret_url=false') 
      .'">' 
      .'</param>' 
      .'<param name="allowscriptaccess" value="always">' 
      .'</param>' 
      .'<embed allowscriptaccess="always" height="81" src="' 
       .urlencode('http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Ftheshiverman%2Fsummer-beats-july-2010&secret_url=false') 
       .'" type="application/x-shockwave-flash" width="100%">' 
      .'</embed>' 
     .'</object>'; 

function JustPrint($parser,$data) 
{ 
    print $data; 
} 

function OpenTag($parser,$name ,$attribs) 
{ 
    // For special tags add a new attribute. 
    if (strcasecmp($name, "object") == 0) 
    { 
     $attribs['Martin'] = 'York'; 
    } 


    // Print the tag. 
    print "<$name "; 
    foreach ($attribs as $loop => $value) 
    { 
     print "$loop=\"$value\" "; 
    } 
    print ">\n"; 
} 

function CloseTag($parser,$name) 
{ 
    print "<$name/>\n"; 
} 

$xmlParser = xml_parser_create(); 
xml_set_default_handler($xmlParser ,'JustPrint' ); 
xml_set_element_handler($xmlParser, 'OpenTag' , 'CloseTag' ); 
xml_parse($xmlParser, $test); 

?> 
+0

啊,我想這可能是。我很難找出用來匹配哪些字符。將「(。*)」匹配「http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Ftheshiverman%2Fsummer-beats-july-2010&secret_url=false」 ?我有點認爲這是兩個,但我正則表達式真的讓我困惑。 – 2010-08-17 06:05:46

+0

看看:http://php.net/manual/en/book.xml.php – 2010-08-17 06:09:57

+0

我不知道我在找什麼鏈接。 – 2010-08-17 06:16:38

0

我不想操縱它(如果可能只需更換高度值。我希望它留酷似它,我使用正則表達式來mimimise SQL注入,並確保它是一個?嵌入代碼

能不只是像一個字符串處理,準確地保持原樣,但反對的東西檢查

舉例來說,這部作品與YouTube嵌入鏈接:

/preg_match(<object width=\"([0-9]*)\" height=\"([0-9]*)\"><param name=\"movie\" value=\"(.*)\"><\/param><param name=\"allowFullScreen\" value=\".*\"><\/param><param name=\"allowscriptaccess\" value=\".*\"><\/param><embed src=\".*\" type=\".*\" allowscriptaccess=\".*\" allowfullscreen=\".*\" width=\"[0-9]*\" height=\"[0-9]*\"><\/embed><\/object>/',$test,$preg_out) 

的preg_match [0] 的preg_match [1] 的preg_match [3]

返回的寬度和高度的對象的url。

1

這是我所用,通過更換$url無論VAR你使用:

if (strtolower(str_ireplace('www.', '', parse_url($url, PHP_URL_HOST))) == 'soundcloud.com') { ?> 
    <embed id="swf_u621112_1" width="890" height="84" flashvars="width=398&height=84" wmode="opaque" salign="tl" allowscriptaccess="never " allowfullscreen="true" scale="scale" quality="high" bgcolor="#FFFFFF" name="swf_u621112_1" style="" src="http://player.soundcloud.com/p layer.swf?url=<?php echo htmlspecialchars(urlencode($url)) ?>" type="application/x-shockwave-flash"> 
<?php 
}