1

For example, I have this string containing some number of iframe tags (but there can be also some text or links, so the point is to select only iframe tags):

<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>

I need to select random iframe tag string from this and I need both opening and closing tag to be included. I suppose I should use something like explode and then use array_rand() function, but there is no divider. Other option that came to my mind is regex, but understanding of that still escapes me.

1
  • 1
    I think you want to parse the HTML. Try HTML parsers or dom crawlers Commented Feb 27, 2014 at 17:33

1 Answer 1

4

Regular expressions are not suitable for parsing HTML. Use a DOM parser instead -- here's a solution using PHP's native DOMDocument class:

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

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

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

In the above code, first a random index between 0 and the total number of tags ($iframes->length) is chosen with mt_rand(), and then the item() method is used to specifically access that tag. Once you have the tag, you can do any further processing. In the demo, I've shown you how to extract the src attribute just to show it's random.

Online demo

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, this is the way :) But I cant figure out, how to print the whole tag...it seems that $random_tag is object so this could be the problem...is there a way to transform it into string?
@MichalS: Sure, it's possible. Simply use echo $dom->saveHTML($random_tag);.
@MichalS: if you're asking about the saveHTML, it is used to create an HTML document (or part of it) from a DOM representation. In this case $random_tag is the object that has all the information required for us to create the tag and using saveHTML() on it would produce the HTML representation of the object (which is the output you're looking for).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.