Cannot Display Chinese Character in my PHP code
Posted
by
Jun1st
on Stack Overflow
See other posts from Stack Overflow
or by Jun1st
Published on 2010-10-10T14:41:25Z
Indexed on
2012/09/23
21:38 UTC
Read the original article
Hit count: 354
I want to display my Twitter Info in my blog. So I write some code to get it.
the issue I got is that Chinese characters displayed as unknown code.
Here is the test code. Could anyone take a look and help? Thanks
<html>
<title>Twitter Test</title>
<body>
<?php
function mystique_objectToArray($object){
if(!is_object($object) && !is_array($object)) return $object;
if(is_object($object)) $object = get_object_vars($object);
return array_map('mystique_objectToArray', $object);
}
define( 'ABSPATH', dirname(dirname(__FILE__)) . '/' );
require_once('/home/jun1st/jun1stfeng.com/wp-includes/class-snoopy.php');
$snoopy = new Snoopy;
$response = @$snoopy->fetch("http://twitter.com/users/show/jun1st.json");
if ($response) $userdata = json_decode($snoopy->results, true); else $error = true;
$response = @$snoopy->fetch("http://twitter.com/statuses/user_timeline/jun1st.json");
if ($response) $tweets = json_decode($snoopy->results, true); else $error = true;
if(!$error):
// for php < 5 (included JSON returns object)
$userdata = mystique_objectToArray($userdata);
$tweets = mystique_objectToArray($tweets);
$twitdata = array();
$twitdata['user']['profile_image_url'] = $userdata['profile_image_url'];
$twitdata['user']['name'] = $userdata['name'];
$twitdata['user']['screen_name'] = $userdata['screen_name'];
$twitdata['user']['followers_count'] = $userdata['followers_count'];
$i = 0;
foreach($tweets as $tweet):
$twitdata['tweets'][$i]['text'] = $tweet['text'];
$twitdata['tweets'][$i]['created_at'] = $tweet['created_at'];
$twitdata['tweets'][$i]['id'] = $tweet['id'];
$i++;
endforeach;
endif;
// only show if the twitter data from the database is newer than 6 hours
if(is_array($twitdata['tweets'])): ?>
<div class="clear-block">
<div class="avatar"><img src="<?php echo $twitdata['user']['profile_image_url']; ?>" alt="<?php echo $twitdata['user']['name']; ?>" /></div>
<div class="info"><a href="http://www.twitter.com/jun1st"><?php echo $twitdata['user']['name']; ?> </a><br /><span class="followers"> <?php printf(__("%s followers","mystique"),$twitdata['user']['followers_count']); ?></span></div>
</div>
<ul>
<?php
$i = 0;
foreach($twitdata['tweets'] as $tweet):
$pattern = '/\@(\w+)/';
$replace = '<a rel="nofollow" href="http://twitter.com/$1">@$1</a>';
$tweet['text'] = preg_replace($pattern, $replace , $tweet['text']);
$tweet['text'] = make_clickable($tweet['text']);
// remove +XXXX
$tweettime = substr_replace($tweet['created_at'],'',strpos($tweet['created_at'],"+"),5);
$link = "http://twitter.com/".$twitdata['user']['screen_name']."/statuses/".$tweet['id'];
echo '<li><span class="entry">' . $tweet['text'] .'<a class="date" href="'.$link.'" rel="nofollow">'.$tweettime.'</a></span></li>';
$i++;
if ($i == $twitcount) break;
endforeach;
?>
</ul>
<? endif?>
?>
</body>
</html>
© Stack Overflow or respective owner