Extraction from string - Ruby

Posted by Dantes on Stack Overflow See other posts from Stack Overflow or by Dantes
Published on 2012-04-14T23:15:09Z Indexed on 2012/04/14 23:28 UTC
Read the original article Hit count: 209

Filed under:
|
|

I have a string. That string is a html code and it serves as a teaser for the blog posts I am creating. The whole html code (teaser) is stored in a field in the database.

My goal: I'd like to make that when a user (facebook like social button) likes certain blog post, right data is displayed on his news feeds. In order to do that I need to extract from the teaser in the first occurrence of an image an image path inside src="i-m-a-g-e--p-a-t-h". I succeeded when a user puts only one image in teaser, but if he accidentally puts two images or more the whole thing craches. Furthermore, for description field I need to extract text inside the first occurrence inside <p> tag. The problem is also that a user can put an image inside the first tag.

I would very much appreciate if an expert could help me resolve this what's been bugging me for days.

Text string with a regular expression for extracting src can be found here: http://rubular.com/r/gajzivoBSf

Thanks!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby