2010-08-17 58 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 
}