[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Remove HTML from String?

jotto

1/8/2006 11:31:00 AM

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.stri...

9 Answers

Horacio Sanson

1/9/2006 12:24:00 PM

0

A regular expression can strip the HTML tags from any string...

I use this

# Get the html data in a string by any method
html_string = get_html_method

# strip all the html tags from the html data
html_string.gsub!(/(<[^>]*>)|\n|\t/s) {" "}


this may not be the best way, (robust or fast) but is enough for my needs.

Horacio

Monday 09 January 2006 17:38?jotto ????????:
> 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.stri...


Austin Ziegler

1/9/2006 12:58:00 PM

0

On 09/01/06, jotto <jonathan.otto@gmail.com> 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.stri...

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{</?[^>]+?>}, '')

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Gavin Kistner

1/9/2006 11:28:00 PM

0

On Jan 9, 2006, at 5:57 AM, Austin Ziegler wrote:
> On 09/01/06, jotto <jonathan.otto@gmail.com> 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.stri...
>
> 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. For
example, watch the above regexp go to town on some invalid HTML:


class String
def strip_tags
self.gsub( %r{</?[^>]+?>}, '' )
end
end

source = <<ENDHTML
<html><body>
<p>I'm pretending to know how to code. I <3 HTML, it's teh best!!!!</p>
<script>
for ( i=0; i<10; i++ ){ document.write(i+'<br>') }
</script>
BLASTOFFS!!!!
</body>
ENDHTML

puts source.strip_tags
#=> I'm pretending to know how to code. I
#=>
#=> for ( i=0; i') }
#=>
#=> BLASTOFFS!!!!


Eric Schwartz

1/9/2006 11:53:00 PM

0

Gavin Kistner <gavin@refinery.com> writes:
> On Jan 9, 2006, at 5:57 AM, Austin Ziegler wrote:
> > On 09/01/06, jotto <jonathan.otto@gmail.com> 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.stri...
> >
> > 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:

<p>a <img src="greaterthan.gif" alt=">" /> b</p>

irb(main):010:0> a='<p>a <img src="greaterthan.gif" alt=">" /> b</p>'
=> "<p>a <img src=\"greaterthan.gif\" alt=\">\" /> b</p>"
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

J. Ryan Sobol

1/10/2006 1:42:00 AM

0

If you're concerned about prevent browsers from rendering the HTML in
your string, replacing < and > with &lt; and &gt; symbols is more
affective than trying to remove the tags.

~ ryan ~



Austin Ziegler

1/10/2006 3:18:00 PM

0

On 09/01/06, Eric Schwartz <emschwar@mail.ericschwartz.us> 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:
>
> <p>a <img src="greaterthan.gif" alt=">" /> b</p>

Actually, that is *not* completely valid, at least not valid XHTML
(which is what I use these days). You have to do that as:

<p>a <img src="greaterthan.gif" alt="&gt;" /> b</p>

But my regexp wasn't intended to be complete; there are full libraries
out there for that.

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Eric Schwartz

1/10/2006 11:27:00 PM

0

Austin Ziegler <halostatue@gmail.com> writes:
> On 09/01/06, Eric Schwartz <emschwar@mail.ericschwartz.us> 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:
> >
> > <p>a <img src="greaterthan.gif" alt=">" /> b</p>
>
> 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-ha...).
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:
> <p>a <img src="greaterthan.gif" alt="&gt;" /> b</p>
>
> 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.

-=Eric

Ian Amuhton

1/11/2006 3:35:00 AM

0

Austin Ziegler wrote:
>> <p>a <img src="greaterthan.gif" alt=">" /> b</p>
>
> Actually, that is *not* completely valid, at least not valid XHTML
> (which is what I use these days). You have to do that as:
> <p>a <img src="greaterthan.gif" alt="&gt;" /> b</p>

Not true. This ...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd...
<html xmlns="http://www.w3.org/1999/xhtml...
<head>
<title>Some Title</title>
</head>
<body>
<p>a <img src="greaterthan.gif" alt=">" /> b</p>
</body>
</html>

validates just fine. Don't trust me, verify it! :)

Bottom line:

You *must* escape `<` and `&` in markup.

You do *not* have to escape `>` (even in attribute values), nor do you
have to escape `'` or `"`. Those are common, but mistaken assumptions.
E.g. with numerous "HTML editors" (MS Frontpage, anyone?) when you type

And Susy said, "Hello world."

The oh-so-kind editor will generously change that in the markup to

<p>And Susy said, &quot;Hello world.&quot;</p>

which, while not actually incorrect, is certainly quite over-the-top
when a plain

<p>And Susy said, "Hello world."</p>

would have done just fine, thank you very much. ...


--
da
~~


RubyTalk@gmail.com

1/11/2006 4:14:00 AM

0