From: ruby talk Date: 2006-01-11T13:13:40+09:00 Subject: Re: Remove HTML from String? ------=_Part_110378_8267316.1136952806778 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline i like this code i found, i did not make but found. and i wish i could give credit to who created it but i lost the website require 'cgi' def html2text html text =3D html. gsub(/( |\n|\s)+/im, ' ').squeeze(' ').strip. gsub(/<([^\s]+)[^>]*(src|href)=3D\s*(.?)([^>\s]*)\3[^>]*>\4<\/\1>/i, '\= 4') links =3D [] linkregex =3D /<[^>]*(src|href)=3D\s*(.?)([^>\s]*)\2[^>]*>\s*/i while linkregex.match(text) links << $~[3] text.sub!(linkregex, "[#{links.size}]") end text =3D CGI.unescapeHTML( text. gsub(/<(script|style)[^>]*>.*<\/\1>/im, ''). gsub(//m, ''). gsub(/]*)>/i, "___\n"). gsub(/]*)>/i, "\n* "). gsub(/]*)>/i, '> '). gsub(/<(br)(| [^>]*)>/i, "\n"). gsub(/<(\/h[\d]+|p)(| [^>]*)>/i, "\n\n"). gsub(/<[^>]*>/, '') ).lstrip.gsub(/\n[ ]+/, "\n") + "\n" for i in (0...links.size).to_a text =3D text + "\n [#{i+1}] <#{CGI.unescapeHTML(links[i])}>" unless links[i].nil? end links =3D nil text end input =3D"

Title

This is the body. Testing link to Google.

Testing image .
The End." print html2text(input) On 1/10/06, Eric Schwartz wrote: > > Austin Ziegler writes: > > On 09/01/06, Eric Schwartz wrote: > > > More like, "Just pray the HTML you are modifying doesn't happen to be > > > completely valid, but not formed in exactly the way you are > > > expecting." For instance, the following HTML snippet is completely > > > valid, but screws up the regex: > > > > > >

a 3D"" /> b

> > > > Actually, that is *not* completely valid, at least not valid XHTML > > (which is what I use these days). > > When wrapped with the appropriate tags, it validated HTML 4.01, which > is what I recommend most people generate these days (because of some, > but not all, of the reasons elucidated at > http://codinginparadise.org/weblog/2005/08/xhtml-considered-harmful.html)= . > So yes, it is valid HTML, which is all I claimed it to be. > > I specifically didn't mention XHTML, since the bits of the thread I > saw referenced HTML, and they're enough different I figured XHTML > would have been mentioned if that's what was wanted. Of course with > XHTML, you have CDATA sections, which can contain all sorts of > nastiness that can trip you up just as badly. > > > You have to do that as: > >

a 3D">" b

> > > > But my regexp wasn't intended to be complete; there are full libraries > > out there for that. > > Right; my point was that in my experience, regexes seem to work just > fine, until suddenly they don't, and then you have to spend silly > amounts of time compensating for them-- or you could just use a proper > library in the first place, and not have to worry about it. > > -=3DEric > > ------=_Part_110378_8267316.1136952806778--