[英]PHP - How to select random tag in the string
例如,我有一个包含一些iframe标签的字符串(但是也可以包含一些文本或链接,因此重点是仅选择iframe标签):
<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&embed_type=widget_standard&embed_uuid=9ff7c333-5c68-40d6-b9c7-b475c6a8d297&hide_tracklist=1&replace=0&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&embed_type=widget_standard&embed_uuid=7186f43a-4bc7-431d-8041-f51366355c44&hide_tracklist=1&replace=0&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&embed_type=widget_standard&embed_uuid=7f2202e6-fd70-45ac-ac1e-6c9dca0ad725&hide_tracklist=1&replace=0&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&embed_type=widget_standard&embed_uuid=e3f68ffd-488d-4d78-b369-a46c785f59a5&hide_tracklist=1&replace=0&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&embed_type=widget_standard&embed_uuid=2c80035e-27e8-4321-b07d-395e6777b98c&hide_tracklist=1&replace=0&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&embed_uuid=f81d24a4-c2f8-4bc5-a10f-7f3fb2243392&stylecolor=&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&embed_uuid=e63685e9-901c-4d71-a1c5-69d0afb130d6&stylecolor=&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&embed_uuid=5a7e4685-cf6a-4f84-ba1c-13251d5b7f59&stylecolor=&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&embed_uuid=7818bedc-94d0-46b1-8193-4cafcf65ffb5&stylecolor=&embed_type=widget_standard" width="480"></iframe></p>
我需要从中选择随机的iframe标签字符串,并且需要同时包含开始和结束标签。 我想我应该使用诸如explode之类的东西,然后使用array_rand()
函数,但是没有分隔符。 我想到的另一种选择是正则表达式,但是对它的理解仍然使我无所适从。
正则表达式不适合解析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);
在上面的代码中,首先使用mt_rand()
选择介于0和标签总数( $iframes->length
)之间的随机索引,然后使用item()
方法专门访问该标签。 获得标签后,您可以进行任何进一步的处理。 在演示中,我向您展示了如何提取src
属性以表明它是随机的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.