Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with PHP charset! 1

Status
Not open for further replies.

zyzjacky

Programmer
Jan 19, 2006
76
MT
i have got a 1.html page that uses a js to request a php file that will echo some html content, and writes the content back in <div></div> of the 1.html(some AJAX ideas) there are some chinese chars includs in that content. but evverything is good except the chinese chars. it shows me some garbage word, like ????, i don't think it's another kind of language. how to solve this problem? i am using utf-8 for html page's charset and set all default charset to utf-8 in my apache server. thanks
 
not sure why this might not be working. have you ensured that the php.ini file you are using is set to use utf_8 by default as, of course, the <meta> tags in your normal php page will not be sent down the return channel from your ajax request (probably). If this is not set then I'm not sure what happens: probably the browser uses its default, which may be different for different browsers. You could also force the charset by setting a header() with the content-type before passing the string to the handler.

as an addition or workaround try encoding the utf string with utf_encode() before sending it down to the ajax handler. when you have the encoded stream, decode it in javascript using any of the base64 decoders that are in the public domain. I have seen this one recommended in the php online manual but have no personal experience of it.
 
the content of php is actuall a <div></div> tag, forget about ajax. it doesn't even show correctly when i just open the php file. all words are turn into a small squre. any ideas?

p.s. thx for your reply, jpadie!!
 
the same basic comment applies.

try the following code

Code:
$mystring = "";//insert your string with utf8 chars in it
header("Content-type: text/css; charset=UTF-8");
echo "<div>$mystring</div";

do you still get the garbage characters (and if so, which browser + version are you using)?
 
problem still there!
i have tried UTF-8 and gb2312 for charset, and tried text/html instead of text/css, but no luck.
browsers i am using is IE 60. and IE 7. neither of them doesn't work....
 
sorry, I meant text/html.

please give me an example of a chinese string you're working with. please encode it with base64encode() before posting it (as i have no idea what charset the tek-tips database uses).


 
thanks man.

??
not sure what base64encode() is, sorry ^^
 
Code:
$fname = "tktips.txt";
$fh = fopen($fname, "w");

$mystring = "";//some chinese characters
fwrite($fh, base64_encode($mystring);
fclose ($fh);
then copy and paste the contents of tktips.txt or upload the file at (ignore that it says csv file).
 
done and i also want to post the code, so you can get more clear idea about this.

filename:index.htm
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" language="javascript" src="script.js"></script>
<LINK href="style.css" rel=stylesheet>
<title></title>
</head>

<body>
<div class="customerBody">
	<div class="leftMenuBar">
		<div class="leftMenuTitle" onclick="hideItem('basicinfo')">???main menu</div>
		<ul class="leftMenuItemUL" id="basicinfo">
			<li class="adminleftMenuItem" onclick="makeRequest('feedback.php')">??1(Menu1)</li>
		</ul>
	</div>
	<div class="customerContent" id="cpBody">
	</div>
	</div>
</div>

</body>
</html>

filename:style.css
Code:
body{
	margin-left:0px;
	margin-top:0px;
	background-color:#fff;
	font-family:Verdana, ??;
	font-size:12px;
	color:#000;
}

.customerBody{
	width: 760px;
	height: 480px;
}

.leftMenuBar{
	width: 150px;
	margin-top: 15px;
	float:left;
}

.leftMenuTitle{
	width: 140px;
	margin-bottom: 1px;
	background-color: #cadec9;
	padding-left: 5px;
	padding-right: 5px;
	padding-top: 5px;
	padding-bottom: 2px;
	font-weight:bold;
	display:block;
	cursor:pointer;
	color:#5382a1;
}

.leftMenuItemUL{
	width:140px;
	list-style:none;
	padding: 0px;
	margin: 0px;
}

.displayNone{
	display:none;
}

.adminleftMenuItem{
	width: 140px;
	margin-bottom: 1px;
	background-color: #eaf2ea;
	padding-left: 10px;
	padding-top: 5px;
	padding-bottom: 2px;
	display: block;
	cursor: pointer;
}

filename:script.js
Code:
function hideItem(menuSet){
	if (document.getElementById(menuSet).className=="displayNone")
	{
		document.getElementById(menuSet).className="leftMenuItemUL";
	}else
	{
		document.getElementById(menuSet).className="displayNone";
	}
}

/*Ajax Functions*/
function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

//Make XML Request
function makeRequest(url) {
    var http_request = createRequestObject();
    if (!http_request) 
	{
          alert('Cannot create an XMLHTTP instance');
          return false;
    }
    http_request.onreadystatechange = function() 
	{   
	    alertContents(http_request); 
	};
    http_request.open('GET', url, true);
    http_request.send(null);
}

//Actions after get a server response
function alertContents(http_request) {
     if (http_request.readyState == 4) {//receive done!
            if (http_request.status == 200) {//http correct
				//process back texts
				document.getElementById("cpBody").innerHTML=http_request.responseText;
            } else {
                alert('There was a problem with the request.');
            }
        }

}

filename:feedback.php
Code:
<?
	$mystring = '??';
	//header("Content-type:text/html; charset=gb2312");
	echo "<div>$mystring</div>";
?>

and keep them in the same dir. thanks again!
 
i also upload the font name, ?? in css file to you.
 
thanks. both work fine for me with the following code

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
<style type="text/css">
body {font-family:"HanDing-CS-Fonts"}
</style>
</head>

<body>
<? echo base64_decode("y87M5Q=="); ?>
<br/>
<? echo base64_decode("xOO6ww=="); ?>
</body>
</html>

remember to make sure that you have a proper chinese font installed and that you use css to tell the browser to use it for the display.

for interest: here are the characters that were displayed in my browser. i'm unsure whether tek-tips will render them properly.

??
??
 
Thanks, it works when we just open the php file, even without base64_decode. but it still doesn't work when ajax gets the echo content and display inside my htm file. i've never hated "?" like this before. could you try the code that i posted out?
 
if i change feedback.php to the following:
Code:
<?
    $mystring = base64_decode("y87M5Q==");
    header("Content-type:text/html; charset=gb2312");
    echo "<div>$mystring</div>";
?>
your code works just fine. if i comment the header out my php installation tries to send the data using the iso8859 char set

note that the only reason i am using base64_decode is because i don't have the actual character string you are using. since you have it natively, you should not need this bit.

the code does not work (in FF) if you change the charset to utf-8. not sure why.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top