Results 1 to 2 of 2

Thread: Swedish chars becoming ?

  1. #1
    Join Date
    Mar 2007
    Posts
    32
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Swedish chars becoming ?

    I have a small simple php page that basically does something like this:

    Code:
    $sendstring="new Array(";
    $result = mysql_query("select output from xxx",$db) or die("Query sucks!");
    
    while ($myrow=cl_mysql_fetch_array($result)) {
      $sendstring.="\"$output\",";
    }
    $sendstring=substr($sendstring,0,strlen($sendstring)-1);
    $sendstring.=")";
    
    echo "$sendstring";
    And when I just run that page the result comes out as intended with the correct line in the browser. (if I run only the PHP page)

    But when I run it thru my AJAX influensed Javascript, all the Swedish chars becomes ? instead of å,ä or ö.

    Anyone got any idea why Javascript makes this look wierd?

  2. #2
    Join Date
    Mar 2007
    Posts
    32
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    I solved it by doing this in the PHP code:

    Code:
    function cl_html_entity_encode($str){
      return preg_replace('/[^!-%\x27-;=?-~ ]/e', '"&#".ord("$0").chr(59)', $str);
    }
    And changing one of the last lines to:
    Code:
    $sendstring=cl_html_entity_encode(substr($sendstring,0,strlen($sendstring)-1));
    Then added this function to my JavaScript:
    Code:
    function html_entity_decode(str) {
      var ta=document.createElement("textarea");
      ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
      return ta.value;
    }
    Then this further down:
    Code:
      tmpstr=html_entity_decode(xmlHttp.responseText)
      eval("var d="+tmpstr);
    So I basically encode them as HTML entities in PHP, then decode them in Javascript before I put the values into my dropdown box. And it works nicely.

    Just wanted to share if anyone else had the same or similar problems.

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
  •