PDA

View Full Version : Swedish chars becoming ?



Luterin
04-03-2007, 03:37 PM
I have a small simple php page that basically does something like this:


$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?

Luterin
04-03-2007, 10:56 PM
I solved it by doing this in the PHP 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:

$sendstring=cl_html_entity_encode(substr($sendstring,0,strlen($sendstring)-1));

Then added this function to my JavaScript:

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:

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.