PHP-如何在字符串中选择随机标记

PHP-如何在字符串中选择随机标记,php,string,random,Php,String,Random,例如,我有一个字符串,其中包含一些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

例如,我有一个字符串,其中包含一些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本机类的解决方案:

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


正则表达式不适合解析HTML。改用DOM解析器-下面是一个使用PHP本机类的解决方案:

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


我认为您需要解析HTML。试试HTML解析器或dom爬虫谢谢,我会看的。我想你想要解析HTML。试试HTML解析器或dom爬虫谢谢,我会看的。谢谢,这是方法:但我不知道如何打印整个标记…似乎$random_标记是对象,所以这可能是问题…有没有办法将其转换为字符串?@MichalS:当然,有可能。只需使用echo$dom->saveHTML$random_tag@MichalS:如果您询问saveHTML,它用于从DOM表示创建HTML文档或其中的一部分。在本例中,$random_tag是一个对象,它拥有我们创建标记所需的所有信息,并且在它上使用saveHTML将生成该对象的HTML表示,这就是您要查找的输出。谢谢,这是一种方式:但我不明白,如何打印整个标签…似乎$random_标签是对象,所以这可能是问题所在…有没有办法将其转换为字符串?@MichalS:当然,有可能。只需使用echo$dom->saveHTML$random_tag@MichalS:如果您询问saveHTML,它用于从DOM表示创建HTML文档或其中的一部分。在本例中,$random_tag是一个对象,它包含了我们创建标记所需的所有信息,在它上面使用saveHTML将生成对象的HTML表示,这就是您要查找的输出。
$dom = new DOMDocument;
$dom->loadHTML($html);
$iframes = $dom->getElementsByTagName('iframe');

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

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