2014-02-27 54 views
1

例如,我有这个字符串包含一些iframe标记(但也可以有一些文本或链接,所以点只选择iframe标记):PHP - 如何选择字符串中的随机标记

<p><iframe frameborder="0" height="180" src="http://www.mixcloud.com/widget/iframe/?feed=http%3A%2F%2Fwww.mixcloud.com%2Fskaph%2Fskaphchapeau-rouge-2022014%2F&amp;embed_type=widget_standard&amp;embed_uuid=9ff7c333-5c68-40d6-b9c7-b475c6a8d297&amp;hide_tracklist=1&amp;replace=0&amp;hide_cover=1" width="600" ></iframe></p> 

<p><iframe frameborder="0" height="180" src="http://www.mixcloud.com/widget/iframe/?feed=http%3A%2F%2Fwww.mixcloud.com%2Fskaph%2Fx-tract-podcast-night-30-skaph%2F&amp;embed_type=widget_standard&amp;embed_uuid=7186f43a-4bc7-431d-8041-f51366355c44&amp;hide_tracklist=1&amp;replace=0&amp;hide_cover=1" width="600" ></iframe></p> 

<p><iframe frameborder="0" height="180" src="http://www.mixcloud.com/widget/iframe/?feed=http%3A%2F%2Fwww.mixcloud.com%2Fskaph%2Fskaphclick-clack-07122013-experiment-liberec%2F&amp;embed_type=widget_standard&amp;embed_uuid=7f2202e6-fd70-45ac-ac1e-6c9dca0ad725&amp;hide_tracklist=1&amp;replace=0&amp;hide_cover=1" width="600" ></iframe></p> 

<p><iframe frameborder="0" height="180" src="http://www.mixcloud.com/widget/iframe/?feed=http%3A%2F%2Fwww.mixcloud.com%2FTFSpodcast%2Ftechno-for-soul-podcast-11-mixed-by-skaph%2F&amp;embed_type=widget_standard&amp;embed_uuid=e3f68ffd-488d-4d78-b369-a46c785f59a5&amp;hide_tracklist=1&amp;replace=0&amp;hide_cover=1" width="600" ></iframe></p> 

<p><iframe frameborder="0" height="180" src="http://www.mixcloud.com/widget/iframe/?feed=http%3A%2F%2Fwww.mixcloud.com%2Fskaph%2Fskaphtechno-je-v%C5%A1echno-5%2F&amp;embed_type=widget_standard&amp;embed_uuid=2c80035e-27e8-4321-b07d-395e6777b98c&amp;hide_tracklist=1&amp;replace=0&amp;hide_cover=1" width="600" ></iframe></p> 

<p><iframe frameborder="0" height="132" src="http://www.mixcloud.com/widget/iframe/?feed=http%3A%2F%2Fwww.mixcloud.com%2Fskaph%2Fskaphtechno-je-v%25C5%25A1echno-vol-2-liberec-experiment-18052013%2F&amp;embed_uuid=f81d24a4-c2f8-4bc5-a10f-7f3fb2243392&amp;stylecolor=&amp;embed_type=widget_standard" width="480"></iframe></p> 

<p><iframe frameborder="0" height="132" src="http://www.mixcloud.com/widget/iframe/?feed=http%3A%2F%2Fwww.mixcloud.com%2Fskaph%2Fskaphexperiment-18012013%2F&amp;embed_uuid=e63685e9-901c-4d71-a1c5-69d0afb130d6&amp;stylecolor=&amp;embed_type=widget_standard" width="480"></iframe></p> 

<p><iframe frameborder="0" height="132" src="http://www.mixcloud.com/widget/iframe/?feed=http%3A%2F%2Fwww.mixcloud.com%2Fskaph%2Fskaph-renaissance-winter-mix-2012%2F&amp;embed_uuid=5a7e4685-cf6a-4f84-ba1c-13251d5b7f59&amp;stylecolor=&amp;embed_type=widget_standard" width="480"></iframe></p> 

<p><iframe frameborder="0" height="132" src="http://www.mixcloud.com/widget/iframe/?feed=http%3A%2F%2Fwww.mixcloud.com%2Fskaph%2Fskaph-mini-technik%2F&amp;embed_uuid=7818bedc-94d0-46b1-8193-4cafcf65ffb5&amp;stylecolor=&amp;embed_type=widget_standard" width="480"></iframe></p> 

我需要从中选择随机iframe标记字符串,我需要打开和关闭标记都包括在内。我想我应该使用类似爆炸的东西,然后使用array_rand()函数,但没有分隔符。我想到的其他选项是正则表达式,但对此的理解仍然让我感到难以置信。

+1

我想你想解析HTML。尝试HTML解析器或dom爬虫 –

+0

谢谢,我会看看它。 –

回答

4

正则表达式不适合解析HTML。使用DOM解析器代替 - 这里的使用PHP的天然DOMDocument类的溶液:

$dom = new DOMDocument; 
$dom->loadHTML($html); 
$iframes = $dom->getElementsByTagName('iframe'); 

$index = mt_rand(0, $iframes->length); 

$random_tag = $iframes->item($index); 

在上面的代码中,0和标签的总数($iframes->length)之间的第一随机索引选择具有mt_rand(),然后item()方法用于专门访问该标签。一旦你有了标签,你可以做任何进一步的处理。在演示中,我向您展示了如何提取src属性,以显示它的随机性。

Online demo

+0

谢谢,这是方式:)但我无法弄清楚,如何打印整个标签......似乎$ random_tag是对象,所以这可能是问题......是否有方法将其转换为字符串? –

+0

@MichalS:当然,这是可能的。只需使用'echo $ dom-> saveHTML($ random_tag);'。 –

+0

不错!现在它的作品:)非常感谢! –

相关问题