dongyuanliao6204 2010-10-10 14:41
浏览 315

在我的PHP代码中无法显示中文字符

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>
  • 写回答

3条回答 默认 最新

  • dongluan6784 2010-10-10 14:44
    关注

    The PHP UTF-8 cheatsheet is good tutorial when displaying content different languages.

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决