From: 7stud -- Date: 2009-07-20T06:45:57+09:00 Subject: Re: Hpricot - strip out images and show first x words Whoops! You wanted 25 *words*: require "rubygems" require "hpricot" html =<
hello:

first paragraph of longer text apple apple apple apple apple apple apple apple apple apple pear pear pear pear pear pear pear pear pear pear pear pear ball ball ball ball ball ball ball ball ball ball ball ball

second paragraph

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