I have a ROR app and I would like a little help regarding parsing in Ruby. I have a field that accepts embed code from vimeo. The issue is that the standard code includes and <iframe> in addition to <p> elements. I would like to know if there is a quick way to remove the keep the <iframe> code while stripping out the code following the <p> element.
For example: Here is the code supplied by vidmeo <iframe src="http://player.vimeo.com/video/28638402?title=0&byline=0&portrait=0&color=ffffff" width="400" height="170" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe><p><a href="http://vimeo.com/28638402">Youth Lagoon - Montana</a> from <a href="http://vimeo.com/tylertwilliams">Tyler T Williams</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
I would like to strip out the code following the
elements, and be left with the following:
<iframe src="http://player.vimeo.com/video/28638402?title=0&byline=0&portrait=0&color=ffffff" width="400" height="170" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
Is there a function in ruby, not as complicated as a regex parse, that would allow assist me ? Or is parsing my best bet? I would appreciate you feedback, and any help you can provide. Thank you very much for your time and help!
I’d recommend that you use an HTML parser such as Nokogiri. Something simple like this:
Simple, reliable, and easily quick enough.