Claire
Claire

Reputation: 3775

How to stop my twitter code outputting apostrophes as '

I am using the below functions to create a twitter feed. You can see the results in the footer of my portfolio at nicolaelvin.com. How do I get rid of that ' and make it an apostrophe?

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>';
        }

    }

Upvotes: 0

Views: 639

Answers (2)

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29985

You're using an extremely outdated API endpoint, you're using RSS and not properly using functions such as htmlentities.

Recommend using the correct API endpoint (https://api.twitter.com/1/statuses/user_timeline.json), using JSON instead of RSS, and not bothering with htmlentities until you're actually showing the data.

Oh, btw, you're using an URL regex on a tweet, but since all URLs are now t.co URLs, there's no need to bother with looking for URLs that don't start with http://

Upvotes: 1

webber2k6
webber2k6

Reputation: 648

I don't know what is exactly the problem. But I think the encoding is caused by the "htmlentities()" function. Look here for correct syntax.

Upvotes: 1

Related Questions