[Rails] Convert "é" in "é"

oo00oo oo00oo at free.fr
Sun Jan 1 14:39:17 GMT 2006


Thanks for reply. I write this little function to do what I need.

<%= getStringEncodedToHtml ( "héhà" ) %> ( function below )
become
h&eacute;h&agrave;

I'm new to RoR and Ruby. Someone could tell me if this function could be 
more optimized ?
I use has_key but perhaprs there is a faster method.

Thanks


def getStringEncodedToHtml ( pString )
    
    str = String.new( pString )
    
    map = {
        34 => "quot",        # "
        38 => "amp",        # &
        39 => "apos",        # '
        60 => "lt",            # <
        62 => "gt",            # >
        169 => "copy",        # ©
        171 => "laquo",        # «
        174 => "reg",        # ®
        187 => "raquo",        # »
        192 => "Agrave",    # À
        194 => "Acirc",        # Â
        198 => "AElig",        # Æ
        199 => "Ccedil",    # Ç
        200 => "Egrave",    # È
        201 => "Eacute",    # É
        202 => "Ecirc",        # Ê
        203 => "Euml",        # Ë
        206 => "Icirc",        # Î
        207 => "Iuml",        # Ï
        212 => "Ocirc",        # Ô
        214 => "Ouml",        # Ö
        217 => "Ugrave",    # Ù
        219 => "Ucirc",        # Û
        220 => "Uuml",        # Ü
        224 => "agrave",    # à
        225 => "aacute",    # á
        226 => "acirc",        # â
        228 => "auml",        # ä
        230 => "aelig",        # æ
        231 => "ccedil",    # ç
        232 => "egrave",    # è
        233 => "eacute",    # é
        234 => "ecirc",        # ê
        235 => "euml",        # ë
        238 => "icirc",        # î
        239 => "iuml",        # ï
        244 => "ocirc",        # ô
        246 => "ouml",        # ö
        249 => "ugrave",    # ù
        251 => "ucirc",        # û
        252 => "uuml",        # ü
    }

    lng = str.length
    while lng > 0
        if map.has_key?( str[ lng ] )
            str[ lng ] = "&" + map[ str[ lng ] ] + ";"
        end
        lng -= 1
    end
    
    return str
end





> If you're using UTF-8, you can try my tip for converting UTF-8  
> characters in to numerical
> HTML entities. These take the form of &#XXXX; and describe a unicode  
> character code;
> works the same way as named entities like &eacute; do.
>
> See http://rails.techno-weenie.net/tip/2005/12/30/ 
> unicode_escaping_h_variant
>
> -Thomas
>
>
> Am 30.12.2005 um 20:46 schrieb oo00oo:
>
>> I try to find an function to convert characters in html chars. "é"  
>> become "&eacute;"
>>
>> There is something in RoR 1.0 to do that ?
>> _______________________________________________
>> Rails mailing list
>> Rails at lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>



More information about the Rails mailing list