0

In my app i have article model, and there i have content field, where are stored all my html data of news articles, like image video etc...

but now i need to format rss feed, and i have to fetch all this img tag, and place them to other xml branch.

for example content:

<h1>asdasd</h1>
content...
<img ... />

and my rss builder view now is such:

xml.instruct! :xml, :version => "1.0"#, :encoding => "windows-1251" 
xml.rss :version => "2.0" do
xml.channel do
for article in @posts
      xml.item do
        xml.title article.title
xml.description article.intro_text
end
end
end

maybe use something like gsub, regex? or how it is better to do? please give an advice. Thank you.

brabertaser19
  • 5,678
  • 16
  • 78
  • 184

1 Answers1

0

You can use the String#scan method, which returns an array of strings that match the provided regex pattern. You would just have to come up with the pattern to match the image tags you want to pull out.

a = "cruel world"
a.scan(/\w+/)        #=> ["cruel", "world"]
a.scan(/.../)        #=> ["cru", "el ", "wor"]
a.scan(/(...)/)      #=> [["cru"], ["el "], ["wor"]]
a.scan(/(..)(..)/)   #=> [["cr", "ue"], ["l ", "wo"]]

For matching the img tags, you could try this pattern which I copied from another SO answer.

<img\s[^>]*?src\s*=\s*['\"]([^'\"]*?)['\"][^>]*?>

This pattern seems to work as per a rubular test.

Community
  • 1
  • 1
animatedgif
  • 1,060
  • 12
  • 16