The Hawk
The Hawk

Reputation: 1568

Twitter API, Adding Hyperlinks

I'm using the Twitter API to grab the last 3 tweets of my timeline using "statuses/user_timeline" function. It grabs the tweets and sends the response, but I need to inject hyperlinks for the HTML in my web page. The problem is the Microsoft characters are throwing off the indices for the location of the links.

Join us at TOMORROW at @SugarLandtxgov's “Art In The Park” and see the 4  
#PODA2 containers all in one place! http://t.co/leqtqhdAiI #HOUarts

[text] => Join us at TOMORROW at @SugarLandtxgov's “Art In The Park” and
see the 4 #PODA2 containers all in one place! http://t.co/leqtqhdAiI 
#HOUarts

[url] => http://t.co/leqtqhdAiI
[expanded_url] => http://www.artshound.com/event/detail/441912984
[display_url] => artshound.com/event/detail/4…
[indices] => Array
       (
              [0] => 109
              [1] => 131
       )

This is the output after I inject the hyperlinks into the string:

Join us at TOMORROW at @SugarLandtxgov's “Art In The Park” and see the 4 
#PODA2 containers all in one pla<a href="http://t.co/leqtqhdAiI" 
target="_blank">ce! http://t.co/leqtqh</a>dAiI #HOUarts

Upvotes: 0

Views: 335

Answers (1)

RonaldPK
RonaldPK

Reputation: 760

You may need to explicitly set the encoding and use multi-byte string functions:

mb_internal_encoding('UTF-8');
header('Content-type: text/html; charset=UTF-8');
echo mb_substr($text, 0, 109) . '<a href="' . $link . '">' . mb_substr($text, 109, 131-109) . '</a>' . mb_substr($text, 131);

Upvotes: 1

Related Questions