[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

strip tags?

Max Benjamin

7/23/2006 6:46:00 AM

Is there an easy way to strip html tags from strings?
Thanks

--
Posted via http://www.ruby-....

10 Answers

Harold Hausman

7/23/2006 7:33:00 AM

0

On 7/23/06, Max Benjamin <moore.joseph@gmail.com> wrote:
> Is there an easy way to strip html tags from strings?
> Thanks
>
> --
> Posted via http://www.ruby-....
>
>

How about using Lynx?
http://lynx.isc.org/lynx2.8.5/...

hth,
-Harold

Stefan Scholl

7/23/2006 7:42:00 AM

0

Max Benjamin <moore.joseph@gmail.com> wrote:
> Is there an easy way to strip html tags from strings?

A regex isn't always the _best_ way to deal with markup
languages, but for an _easy_ way it's good enough.


$ irb
irb(main):001:0> a = '<strong>This is strong stuff!</strong><br><img src="foo.png" alt="Some foo">'
=> "<strong>This is strong stuff!</strong><br><img src=\"foo.png\" alt=\"Some foo\">"
irb(main):002:0> a.gsub(/<.*?>/, '')
=> "This is strong stuff!"

Andreas S.

7/23/2006 8:29:00 AM

0

Daniel Baird wrote:
> On 7/23/06, Stefan Scholl <stesch@no-spoon.de> wrote:
>>
>> Max Benjamin <moore.joseph@gmail.com> wrote:
>> > Is there an easy way to strip html tags from strings?
>>
>> A regex isn't always the _best_ way to deal with markup
>> languages, but for an _easy_ way it's good enough.
>
>
> the problem is, it's not always the _correct_ way.
>
> <div id="weird>id"></div>

This is no correct HTML, < and > have to be encoded as entities.

--
Posted via http://www.ruby-....

Max Benjamin

7/23/2006 4:18:00 PM

0

Daniel Baird wrote:
> On 7/23/06, Andreas S. <f@andreas-s.net> wrote:
>>
>> Daniel Baird wrote:
>> > On 7/23/06, Stefan Scholl <stesch@no-spoon.de> wrote:
>> >> A regex isn't always the _best_ way to deal with markup
>> >> languages, but for an _easy_ way it's good enough.
>> >
>> > the problem is, it's not always the _correct_ way.
>> > <div id="weird>id"></div>
>>
>> This is no correct HTML, < and > have to be encoded as entities.
>
>
> That is true.. if the original poster has the luxury of only dealing
> with
> correct html, he's a lucky fellow, and can kludge up some regexen that
> will
> do the job. Even in a well-coded site, it's not unthinkable that you
> could
> forget to do some encoding and end up with angle-brackets inside a
> textarea
> or something, though.
>
> How useful a regex approach is depends on the data. I have used a bit
> of
> regex-type html parsing before and it worked fine, for the data that I
> was
> parsing. Horses for courses.
>
> ;D
Thanks for the quick replys.
I should have been more explicit in my question. I want to strip html
tags in order to sanitize form input. I'm a bit of a ruby noob and I
was hoping to find a function similar to PHP's strip_tags, one that
would remove both html and ruby code.
Best

--
Posted via http://www.ruby-....

Christian Neukirchen

7/23/2006 4:26:00 PM

0

"Andreas S." <f@andreas-s.net> writes:

> Daniel Baird wrote:
>> On 7/23/06, Stefan Scholl <stesch@no-spoon.de> wrote:
>>>
>>> Max Benjamin <moore.joseph@gmail.com> wrote:
>>> > Is there an easy way to strip html tags from strings?
>>>
>>> A regex isn't always the _best_ way to deal with markup
>>> languages, but for an _easy_ way it's good enough.
>>
>>
>> the problem is, it's not always the _correct_ way.
>>
>> <div id="weird>id"></div>
>
> This is no correct HTML, < and > have to be encoded as entities.

It's valid XHTML:

$ echo '<bar quux="foo>bar" />' | xmllint -
<?xml version="1.0"?>
<bar quux="foo&gt;bar"/>

However, '<' needs to be escaped:

$ echo '<bar quux="foo<bar" />' | xmllint -
-:1: parser error : Unescaped '<' not allowed in attributes values
<bar quux="foo<bar" />

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...

Mat Schaffer

7/23/2006 6:33:00 PM

0


On Jul 23, 2006, at 12:17 PM, Max Benjamin wrote:
> Thanks for the quick replys.
> I should have been more explicit in my question. I want to strip html
> tags in order to sanitize form input. I'm a bit of a ruby noob and I
> was hoping to find a function similar to PHP's strip_tags, one that
> would remove both html and ruby code.
> Best

For sanitizing input, just escaping might be a better idea because it
has less chance of being destructive. If you're on rails there's an h
() function for this. If you're doing something else, maybe check
out how rails does it and replicate it. There might be something
easy that someone on this list knows that don't.

If you really want to strip them, I'd bet the regexp solution is no
less effective than PHP's strip_tags.
-Mat

Max Benjamin

7/23/2006 7:33:00 PM

0

Mat Schaffer wrote:
> On Jul 23, 2006, at 12:17 PM, Max Benjamin wrote:
>> Thanks for the quick replys.
>> I should have been more explicit in my question. I want to strip html
>> tags in order to sanitize form input. I'm a bit of a ruby noob and I
>> was hoping to find a function similar to PHP's strip_tags, one that
>> would remove both html and ruby code.
>> Best
>
> For sanitizing input, just escaping might be a better idea because it
> has less chance of being destructive. If you're on rails there's an h
> () function for this. If you're doing something else, maybe check
> out how rails does it and replicate it. There might be something
> easy that someone on this list knows that don't.
>
> If you really want to strip them, I'd bet the regexp solution is no
> less effective than PHP's strip_tags.
> -Mat

Thanks for the help everybody.
Best,
Max

--
Posted via http://www.ruby-....

William James

7/24/2006 4:18:00 AM

0

Christian Neukirchen wrote:
> "Andreas S." <f@andreas-s.net> writes:
>
> > Daniel Baird wrote:
> >> On 7/23/06, Stefan Scholl <stesch@no-spoon.de> wrote:
> >>>
> >>> Max Benjamin <moore.joseph@gmail.com> wrote:
> >>> > Is there an easy way to strip html tags from strings?
> >>>
> >>> A regex isn't always the _best_ way to deal with markup
> >>> languages, but for an _easy_ way it's good enough.
> >>
> >>
> >> the problem is, it's not always the _correct_ way.
> >>
> >> <div id="weird>id"></div>
> >
> > This is no correct HTML, < and > have to be encoded as entities.
>
> It's valid XHTML:
>
> $ echo '<bar quux="foo>bar" />' | xmllint -
> <?xml version="1.0"?>
> <bar quux="foo&gt;bar"/>
>
> However, '<' needs to be escaped:
>
> $ echo '<bar quux="foo<bar" />' | xmllint -
> -:1: parser error : Unescaped '<' not allowed in attributes values
> <bar quux="foo<bar" />
>
> --
> Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...

re = %r{
<
(?:
# Any characters but > or " .
[^>"] +
|
# Characters within quotes.
# Allow escaped quotes.
"
(?:
# Accept any escaped character.
\\.
|
[^"\\] +
) *
"
) *
>
}xm

print DATA.read.gsub( re, '' )

__END__
Some<><"">
<bar quux="\"foo>bar" /> text
to <?xml version="1.0"?>
<bar quux="foo&gt;bar"/> save
for <bar quux="foo<bar" />
<bar quux="\"foo><bar>" />reading.

Christian Neukirchen

7/25/2006 10:47:00 AM

0

"William James" <w_a_x_man@yahoo.com> writes:

> re = %r{
> <
> (?:
> # Any characters but > or " .
> [^>"] +
> |
> # Characters within quotes.
> # Allow escaped quotes.
> "
> (?:
> # Accept any escaped character.
> \\.
> |
> [^"\\] +
> ) *
> "
> ) *
> >
> }xm
>
> print DATA.read.gsub( re, '' )

<foo bar='"quux"' />

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...

William James

7/25/2006 6:26:00 PM

0

Christian Neukirchen wrote:
> "William James" <w_a_x_man@yahoo.com> writes:
>
> > re = %r{
> > <
> > (?:
> > # Any characters but > or " .
> > [^>"] +
> > |
> > # Characters within quotes.
> > # Allow escaped quotes.
> > "
> > (?:
> > # Accept any escaped character.
> > \\.
> > |
> > [^"\\] +
> > ) *
> > "
> > ) *
> > >
> > }xm
> >
> > print DATA.read.gsub( re, '' )
>
> <foo bar='"quux"' />

re = %r{
<
(?:
[^>"'] +
|
"
(?: \\. | [^\\"] + ) *
"
|
'
(?: \\. | [^\\'] + ) *
'
) *
>

}xm

print DATA.read.gsub( re, '' )

__END__
Some<><"">
<bar quux='"foo>bar' /> text
to <?xml version="1.0"?>
<bar quux="foo>bar"/> save
for <bar quux="foo<bar" />
<foo bar='"quux"' /> later
<bar quux="\"foo><bar>" />reading.