From: Eric Schwartz Date: 2006-01-10T09:03:17+09:00 Subject: Re: Remove HTML from String? Gavin Kistner writes: > On Jan 9, 2006, at 5:57 AM, Austin Ziegler wrote: > > On 09/01/06, jotto wrote: > >> I can't find a method to remove HTML from a string in the core > >> API. PHP > >> has something called strip_tags. Does Ruby have anything like this? > >> http://us3.php.net/manual/en/function.strip-tags.php > > > > Not built in. It's not really appropriate for the core language. > > That's one of the things that makes PHP easy to use for people who are > > trying to do simple things, but makes it hard when you get into > > engineering and maintaining real programs. As was suggested by the > > other respondent, it's relatively easy to remove: > > > > a.gsub(%r{]+?>}, '') > > ..just pray that the HTML you are modifying is valid, and not some > garbage file that web browsers happen to treat as intended. 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 > b

irb(main):010:0> a='

a > b

' => "

a \"\" /> b

" irb(main):011:0> a.gsub(%r{]+?>}, '') => "a \" /> b" Finding other such examples is an exercise for the reader. This sort of thing is why, as a rule, I avoid parsing HTML with regexes. -=Eric