Hello, Is there a common way of stripping html tags from a string? Right now I''m just calling gsub!(/<.*?>/, ''''), but with a background in PHP and always having used its strip_tags() method, I wonder if the Rails community has standardized this fairly common task with something a bit less simpleminded than my quick fix. Thanks! Zack -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060530/f69f748d/attachment.html
On Mon, May 29, 2006 at 07:42:41PM -0700, Zack Ham wrote:> Is there a common way of stripping html tags from a string? Right now I''m just > calling gsub!(/<.*?>/, ''''), but with a background in PHP and always having used > its strip_tags() method, I wonder if the Rails community has standardized this > fairly common task with something a bit less simpleminded than my quick fix.Suspect you want the erb html_escape method http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html#M000688html_escape <%= h string %> -jim
If I escape the html, it just converts the html entities into the &??; form. I want to strip html tags, and end up with a string containing no html tags. This is the PHP equivalent of what I''m talking about: http://us2.php.net/manual/en/function.strip-tags.php Thanks, Zack On 5/29/06, Jim Cheetham <jim@gonzul.net> wrote:> > On Mon, May 29, 2006 at 07:42:41PM -0700, Zack Ham wrote: > > Is there a common way of stripping html tags from a string? Right now > I''m just > > calling gsub!(/<.*?>/, ''''), but with a background in PHP and always > having used > > its strip_tags() method, I wonder if the Rails community has > standardized this > > fairly common task with something a bit less simpleminded than my quick > fix. > > Suspect you want the erb html_escape method > > http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html#M000688html_escape > > <%= h string %> > > -jim > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060530/df64a434/attachment.html
On Tue, May 30, 2006 at 03:51:47AM +0100, Jim Cheetham wrote:> On Mon, May 29, 2006 at 07:42:41PM -0700, Zack Ham wrote: > > Is there a common way of stripping html tags from a string? Right now I''m just > > calling gsub!(/<.*?>/, ''''), but with a background in PHP and always having used > > its strip_tags() method, I wonder if the Rails community has standardized this > > fairly common task with something a bit less simpleminded than my quick fix. > > Suspect you want the erb html_escape methodOr I suspect that I should actually read what you wrote ... :-) http://www.php.net/manual/en/function.strip-tags.php describes the php strip_tags, and points out a number of interesting problems that it might have under some circumstances, and a few good workarounds. Simply gsubbing the <...> instances away isn''t quite the same as rendering the document, but I can''t see anything that does that. I guess finding a "nicer" replacement depends on your source data, and what you''re trying to achieve ... -jim
Well, perhaps strip_tags isn''t perfect, but I''m not necessarily looking for a port here. I''m just looking for a method whose stated goal is to remove html tags from a string. On 5/29/06, Jim Cheetham <jim@gonzul.net> wrote:> > On Tue, May 30, 2006 at 03:51:47AM +0100, Jim Cheetham wrote: > > On Mon, May 29, 2006 at 07:42:41PM -0700, Zack Ham wrote: > > > Is there a common way of stripping html tags from a string? Right now > I''m just > > > calling gsub!(/<.*?>/, ''''), but with a background in PHP and always > having used > > > its strip_tags() method, I wonder if the Rails community has > standardized this > > > fairly common task with something a bit less simpleminded than my > quick fix. > > > > Suspect you want the erb html_escape method > > Or I suspect that I should actually read what you wrote ... :-) > > http://www.php.net/manual/en/function.strip-tags.php describes the php > strip_tags, and points out a number of interesting problems that it > might have under some circumstances, and a few good workarounds. > > Simply gsubbing the <...> instances away isn''t quite the same as > rendering the document, but I can''t see anything that does that. I guess > finding a "nicer" replacement depends on your source data, and what > you''re trying to achieve ... > > -jim > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060530/7e815aeb/attachment.html
http://railsmanual.org/module/ActionView%3A%3AHelpers%3A%3ATextHelper/strip_tags On 5/30/06, Zack Ham <zackham@gmail.com> wrote:> Well, perhaps strip_tags isn''t perfect, but I''m not necessarily looking for > a port here. I''m just looking for a method whose stated goal is to remove > html tags from a string.
Just what I was looking for. This is odd though, I''m running the following html = ActionView::Helpers::TextHelper::strip_tags html And getting a NoMethodError. rails --version tells me I am indeed running 1.1.2 though. Any ideas? Thanks, Zack On 5/29/06, hui <zhengzhengzheng@gmail.com> wrote:> > > http://railsmanual.org/module/ActionView%3A%3AHelpers%3A%3ATextHelper/strip_tags > > On 5/30/06, Zack Ham <zackham@gmail.com> wrote: > > Well, perhaps strip_tags isn''t perfect, but I''m not necessarily looking > for > > a port here. I''m just looking for a method whose stated goal is to > remove > > html tags from a string. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060530/fc8d3690/attachment.html
I''ve been using this ruby sanitize: http://blog.ideoplex.com/2005/03/17.html which lets you specify some tags to allow and strips the rest. cheers, AF On 5/30/06, Zack Ham <zackham@gmail.com> wrote:> > Hello, > > Is there a common way of stripping html tags from a string? Right now I''m > just calling gsub!(/<.*?>/, ''''), but with a background in PHP and always > having used its strip_tags() method, I wonder if the Rails community has > standardized this fairly common task with something a bit less simpleminded > than my quick fix. > > Thanks! > Zack > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- ------------------------------- http://www.aidanf.net ------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060530/432bfa16/attachment.html