簡體   English   中英

PHP-如何在字符串中選擇隨機標記

[英]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&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標簽字符串,並且需要同時包含開始和結束標簽。 我想我應該使用諸如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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM