strip tags?

S

Stefan Scholl

Max Benjamin said:
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!"
 
A

Andreas S.

Daniel said:
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.
 
M

Max Benjamin

Daniel said:
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
 
M

Mat Schaffer

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
 
M

Max Benjamin

Mat said:
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
 
W

William James

Christian said:
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" />

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

William James

Christian said:
William James said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,686
Latest member
sparada

Latest Threads

Top