I’m pretty new to coding and want to try and build a twitter app for my website. With a little help I was able to get all the basics down, but what I’m now struggling with is two things:
- Turning the Twitter XML data into clickable links
- Making sure the XML text renders properly (It seems like “” (quotes) from the XML are being rendered improperly (as ““”).
I seem to have found the answers on how to fix these two things, but being so new to PHP, I’m not exactly sure how to implement these two fixes to my code. Any help would be greatly appreciated.
Solution I found to problem 1:
function twitterify($status) {
$status = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $status);
$status = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $status);
$status = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $status);
$status = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $status);
return $status;
}
Solution I found to problem 2 is to use html_entity_decode… but again I’m just not sure how to implement these two solutions into my code.
My code so far:
<?php
$xmldata = 'https://twitter.com/statuses/user_timeline/carmeloanthony.xml';
$open = fopen($xmldata, 'r');
$content = stream_get_contents($open);
fclose($open);
$xml = new SimpleXMLElement($content);
?>
<table class="table table-striped">
<?php
foreach($xml->status as $status)
{
?>
<tr>
<td> <img src=" <?php echo $status->user->profile_image_url; ?>" /> </td>
<td><strong> <?php echo $status->user->name; ?></strong> <i>@<?php echo $status->user->screen_name; ?></i> <br /> <?php echo $status->text; ?></td>
<td style="width: 40px;"><?php echo date("M j",strtotime($status->created_at)); ?></td>
</tr>
<?php
}
?>
</table>
This is as far as I got…gotta run! All that needs done is to change the regex to find links that are at the end of the line…or precede a newline