From: 7stud -- Date: 2009-07-20T06:23:56+09:00 Subject: Re: Hpricot - strip out images and show first x words Max Williams wrote: > Hey all > > I have a bunch of html in p tags and i want to show shorted summary - > like the first 25 words followed by '...'. I also want to strip out any > images. I'm using hpricot for this and am finding myself writing a > convoluted messy method. Can anyone show me a clean and simple way? Is > Hpricot even the right tool for the job? > See if this helps: require "rubygems" require "hpricot" html =<
hello:

first paragraph of longer text

second paragraph

bye:
ENDOFHTML doc = Hpricot(html) results = [] paras = doc.search("//p") paras.each do |para| para.search("img").remove text = para.inner_html if text.length <= 25 results << text else results << "#{text[0, 25]}..." end end p results ["first paragraph of...", "second paragraph"] -- Posted via http://www.ruby-forum.com/.