使用preg_match过滤掉iframe

时间:2011-08-02 08:46:31

标签: php iframe preg-match

我正在尝试过滤我帖子中的iframe,我的帖子看起来像这样

<!--videoplayer--><iframe title="YouTube video player" class="youtube-player" type="text/html" width="200" height="200" src="http://www.youtube.com/embed/IIYeKGNNNf4?rel=0" frameborder="0" allowFullScreen></iframe><!--endvideoplayer-->Blitz performs at Desifest 2010 in Toronto Canada, & Films music video with Navin Kundra for the song "Love You The Same" feat Kat Eyez & produced by Roach Killa from Blitz's new album "Get Blitz". Join my fanpage for more: WWW.FACEBOOK.COM/BLITZMUSIC *Special thanks to: Deesha, Dj Jump Off, Paul The Drummer, Surgeon, Umar, Navin Kundra, Nyrone, Sats B, Kat Eyez, B Don.  (New Album Coming Soon!)

我只想返回iframe而不是帖子描述,所以从我想要的。

谢谢

3 个答案:

答案 0 :(得分:12)

preg_match('/<iframe.*src=\"(.*)\".*><\/iframe>/isU', $string, $matches);
echo ($matches[0]); //only the <iframe ...></iframe> part
echo ($matches[1]); //the src part. (http://www.youtube.com/embed/IIYeKGNNNf4?rel=0)

答案 1 :(得分:2)

我一遍又一遍地看到相同的错误,使用正则表达式解析html, USE A HTML DOM PARSER

include("simple_html_dom.php") ;
$string = <<< EOF
"<!--videoplayer--><iframe title="YouTube video player" class="youtube-player" type="text/html" width="200" height="200" src="http://www.youtube.com/embed/IIYeKGNNNf4?rel=0" frameborder="0" allowFullScreen></iframe>
<!--endvideoplayer-->Blitz performs at Desifest 2010 in Toronto Canada, & Films music video with Navin Kundra for the song "Love You The Same" feat Kat Eyez & produced by Roach Killa from Blitz's new album "Get Blitz". Join my fanpage for more: WWW.FACEBOOK.COM/BLITZMUSIC *Special thanks to: Deesha, Dj Jump Off, Paul The Drummer, Surgeon, Umar, Navin Kundra, Nyrone, Sats B, Kat Eyez, B Don. (New Album Coming Soon!)"
EOF;

$html = str_get_html($string);     
foreach($html->find('iframe') as $element)
{
       echo $element->src . '<br>';
}

输出:

  

http://www.youtube.com/embed/IIYeKGNNNf4?rel=0

答案 2 :(得分:1)

preg_match('#<iframe(.*?)></iframe>#is', $string, $matches);
echo $matches[1]; // 0 for <iframe></iframe> part.