2011-08-02 97 views
3

我試圖篩選出的iframe在我的崗位過濾掉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不是職位說明,所以從是我想要的是。

謝謝

回答

11
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) 
+0

感謝您的代碼工作 – user874185

+0

正是我所需要的!謝謝!有用! –

+0

這將找到所有iframe,即使它不是Youtube – Gino

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

什麼都不會匹​​配... – technology

+0

啊,我看不出:) –

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

+1

我將保留您的代碼以適應未來。 – user874185