0

我正在使用以下函数来创建 Twitter 提要。您可以在nicolaelvin.com的投资组合页脚中查看结果。我如何摆脱那个 ' 并使它成为撇号?

function twitify($str){
                $str = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $str);
                $str = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $str);
                $str = preg_replace("/@(\w+)/", "@<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">\\1</a>", $str);
                $str = preg_replace("/#(\w+)/", "#<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">\\1</a>", $str);
                return $str;
        }

    function twitter(){

        $twitterRssFeedUrl =  "http://twitter.com/statuses/user_timeline/nicolaElvin.rss";
        $twitterUsername = "nicolaElvin";
        $amountToShow = 5;
        $twitterPosts = false;
        $xml = @simplexml_load_file($twitterRssFeedUrl);
        if(is_object($xml)){
            foreach($xml->channel->item as $twit){
              if(is_array($twitterPosts) && count($twitterPosts)==$amountToShow){
                break;
              }
              $d['title'] = stripslashes(htmlentities($twit->title,ENT_QUOTES,'UTF-8'));
              $description = stripslashes(htmlentities($twit->description,ENT_QUOTES,'UTF-8'));
              if(strtolower(substr($description,0,strlen($twitterUsername))) == strtolower($twitterUsername)){
                $description = substr($description,strlen($twitterUsername)+1);
              }
              $d['description'] = $description;
              $d['pubdate'] = strtotime($twit->pubDate);
              $d['guid'] = stripslashes(htmlentities($twit->guid,ENT_QUOTES,'UTF-8'));
              $d['link'] = stripslashes(htmlentities($twit->link,ENT_QUOTES,'UTF-8'));
              $twitterPosts[]=$d;
            }
        }else{
            die('cannot connect to twitter feed');
        }

        if(is_array($twitterPosts)){


            echo '<ul>';
            foreach($twitterPosts as $post){
                $description=twitify($post['description']);

              echo '<li><time>'.date('F j, Y, g:i a',$post['pubdate']).'</time></li><li>'.$description.'</li>';
            }
            echo '</ul>';
        }else{
            echo '<p>No Twitter posts have been made</p>';
        }

    }
4

2 回答 2

1

我不知道到底是什么问题。但我认为编码是由“htmlentities()”函数引起的。在此处查找正确的语法。

于 2012-05-04T11:46:48.183 回答
1

您正在使用一个非常过时的 API 端点,您正在使用 RSS 并且没有正确使用诸如htmlentities.

建议使用正确的 API 端点 ( ),使用 JSON 而不是 RSS,并且在实际显示数据之前https://api.twitter.com/1/statuses/user_timeline.json不要打扰。htmlentities

哦,顺便说一句,您在推文上使用了 URL 正则表达式,但是由于所有 URL 现在都是 t.co URL,因此无需费心寻找不以 http:// 开头的 URL

于 2012-05-04T12:12:48.610 回答