Forum: Ruby on Rails How to remove special characters? (ie. &)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
yachtman (Guest)
on 2009-02-10 05:35
(Received via mailing list)
This might be such a stupid simple question, so forgive me--I did
search the forum and elsewhere and couldn't find the answer...

I have an HTML text string that formats nicely on a web  page:

s = "Mom & Dad"    => "Mom & Dad"

How do I convert s to just ascii characters, ie. "Mom & Dad"?
stretch (Guest)
on 2009-02-10 06:30
(Received via mailing list)
Yachtman,

Check out http://www.ruby-doc.org/core-1.9/classes/CGI.html#M000527
(It worked with 2.2 also)

>> ugly = CGI::escapeHTML('<BR><A HREF="url"></A>')
=> "&lt;BR&gt;&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt;"

>> print CGI::unescapeElement(ugly, "A", "IMG")
&lt;BR&gt;<A HREF="url"></A>=> nil

>> print CGI::unescapeElement(ugly, "A", "IMG", "BR")
<BR><A HREF="url"></A>=> nil

There might be a higher level function too, but this shows you how you
might use this directly

Al
Robert W. (Guest)
on 2009-02-10 07:26
yachtman wrote:
> This might be such a stupid simple question, so forgive me--I did
> search the forum and elsewhere and couldn't find the answer...
>
> I have an HTML text string that formats nicely on a web  page:
>
> s = "Mom &amp; Dad"    => "Mom & Dad"
>
> How do I convert s to just ascii characters, ie. "Mom & Dad"?

I haven't tried this, but it looks like a robust solution that might
work for you:
http://htmlentities.rubyforge.org/
Alan B. (Guest)
on 2009-02-10 07:35
(Received via mailing list)
Of course, you can't use this to handle all possible encoded strings

On Mon, Feb 9, 2009 at 8:29 PM, stretch <removed_email_address@domain.invalid> 
wrote:

> &lt;BR&gt;<A HREF="url"></A>=> nil
> > This might be such a stupid simple question, so forgive me--I did
> > search the forum and elsewhere and couldn't find the answer...
> >
> > I have an HTML text string that formats nicely on a web  page:
> >
> > s = "Mom &amp; Dad"    => "Mom & Dad"
> >
> > How do I convert s to just ascii characters, ie. "Mom & Dad"?
> >
>


--
Available for Ruby on Rails development opportunities
This topic is locked and can not be replied to.