www.webdeveloper.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2013
    Posts
    1

    Converting utf8 text to html text javascript

    So I started writing some functions to do this but i cannot be the first one to think of this or what to accomplish this...in javascript.

    So for utf8 to html I'm doing something like this

    Code:
    function utf8tohtml(utftxt)
    {
            utftxt = utftxt.replace(/&/g,"&")
            utftxt = utftxt.replace(/Á/g,"Á")
            utftxt = utftxt.replace(/É/g,"É")
            utftxt = utftxt.replace(/Í/g,"Í")
            utftxt = utftxt.replace(/Ó/g,"Ó")
            utftxt = utftxt.replace(/Ú/g,"Ú")
            utftxt = utftxt.replace(/Ý/g,"Ý")
            utftxt = utftxt.replace(/ý/g,"ý")
            utftxt = utftxt.replace(/á/g,"á")
            utftxt = utftxt.replace(/é/g,"é")
            return utftxt
    }
    for html to utf8 i'm doing something like this

    Code:
    function htmltoutf(txt)
    {
            txt = txt.replace(/}/,'}');      // right curly brace
            txt = txt.replace(/~/,'~');      // tilde
            txt = txt.replace(//,'');      // box
            txt = txt.replace(/€/,'€');      // Euro
            txt = txt.replace(/ƒ/,'ƒ');      // florin
            txt = txt.replace(/…/,'…');      // ellipsis
            txt = txt.replace(/ˆ/,'ˆ');      // circumflex accent
            txt = txt.replace(/Š/,'Š');      // 
            txt = txt.replace(/Œ/,'Œ');      // capital OE ligature
            return txt
    }
    and there is more...i've just stopped here for this example...

    appreciate the ideas...

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,783
    Code:
    function escapeExtended(s){
     return s.replace(/([\x80-\xff])/g, function (a, b) {
       var c = b.charCodeAt();
       return "&#" + b.charCodeAt()+";" 
     });
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles