【问题标题】:PHP - How to select random tag in the stringPHP - 如何在字符串中选择随机标签
【发布时间】:2014-02-27 17:31:36
【问题描述】:

例如,我有这个包含一些 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。尝试 HTML 解析器或 dom 爬虫
  • 谢谢,我去看看。

标签: php string random


【解决方案1】:

正则表达式不适合解析 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-&gt;length)之间的随机索引,然后使用item()方法专门访问该标签。获得标签后,您可以进行任何进一步的处理。在演示中,我向您展示了如何提取 src 属性以显示它是随机的。

Online demo

【讨论】:

  • 谢谢,这是方法 :) 但我不知道如何打印整个标签...似乎 $random_tag 是对象所以这可能是问题...是否存在一种将其转换为字符串的方法?
  • @MichalS:当然,这是可能的。只需使用echo $dom-&gt;saveHTML($random_tag);
  • @MichalS:如果您询问saveHTML,它用于从 DOM 表示创建 HTML 文档(或其中的一部分)。在这种情况下,$random_tag 是具有我们创建标记所需的所有信息的对象,并且在其上使用 saveHTML() 将生成对象的 HTML 表示形式(这是您要查找的输出)。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 2020-10-04
相关资源
最近更新 更多