Jump to content

Best way to parse media from content

Ilya Hoilik

Recommended Posts

Hello developers!

Now I'm working on extended streams for my client. He want to see media (YouTube, SoundCloud, Last.fm or Image) from content in each item in the stream.

In general (without custom design) he want to see something like this:

2016-03-15 07-49-37 Скриншот экрана.png

I'm using \DOMDocument. It works correctly. But maybe you have a better approach how to realize it? Or suggestion how to make it easier? Because I'm not sure this is a best way.

This is my code to parse YouTube video:

<!-- Load content from topic row (if this is a topic) -->
{{if $indexData['index_class'] == 'IPS\forums\Topic\Post'}}
	{{$post = \IPS\Db::i()->select( 'post', 'forums_posts', array( 'pid=?', $indexData['index_object_id'] ) )->first();}}

<!-- Create DOMDocument -->
{{$domdoc = new \DOMDocument;}}
{{$domdoc->loadHTML( $post );}}

<!-- Get links to video and preview image -->
{{foreach $domdoc->getElementsByTagName('iframe') as $video}}
    {{if $domdoc->getElementsByTagName('iframe')->length}}
        {{$link = $video->getAttribute('src');}}
        {{$preview = 'http://img.youtube.com/vi/' . explode( '?', explode( '/', $video->getAttribute('src') )[4] )[0] . '/hqdefault.jpg';}}

<!-- Now we can use $link to display YouTube link and $preview to display YouTube preview image -->

Thank you in advance!

Link to comment
Share on other sites

  • 4 weeks later...

I see nothing wrong with your approach, however to keep the template cleaner I might recommend creating a template plugin (under, e.g. system/Output/Plugin/ if you aren't creating a full-blown app) that does this work.  It'll be easier to troubleshoot and debug if anything doesn't work properly, and it'll keep the templates cleaner and easier to read.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...