Skip to content

Commit

Permalink
added Twitter::clickable() - makes twitter links, @usernames and #has…
Browse files Browse the repository at this point in the history
…htags clickable
  • Loading branch information
dg committed Mar 30, 2011
1 parent e305e66 commit 3fcb931
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/load.php
Expand Up @@ -20,7 +20,7 @@
<?php foreach ($channel->status as $status): ?>
<li><a href="http://twitter.com/<?php echo $status->user->screen_name ?>"><img src="<?php echo htmlspecialchars($status->user->profile_image_url) ?>">
<?php echo htmlspecialchars($status->user->name) ?></a>:
<?php echo $status->text ?>
<?php echo Twitter::clickable($status->text) ?>
<small>at <?php echo date("j.n.Y H:i", strtotime($status->created_at)) ?></small>
</li>
<?php endforeach ?>
Expand Down
2 changes: 1 addition & 1 deletion examples/search.php
Expand Up @@ -17,7 +17,7 @@
<?php foreach ($results as $result): ?>
<li><a href="http://twitter.com/<?php echo $result->from_user ?>"><img src="<?php echo htmlspecialchars($result->profile_image_url) ?>" width="48">
<?php echo htmlspecialchars($result->from_user) ?></a>:
<?php echo $result->text ?>
<?php echo Twitter::clickable($result->text) ?>
<small>at <?php echo date("j.n.Y H:i", strtotime($result->created_at)) ?></small>
</li>
<?php endforeach ?>
Expand Down
36 changes: 36 additions & 0 deletions twitter.class.php
Expand Up @@ -277,6 +277,42 @@ public function cachedRequest($request, $data)



/**
* Makes twitter links, @usernames and #hashtags clickable.
* @param string
* @return string
*/
public static function clickable($s)
{
return preg_replace_callback(
'~(?<!\w)(https?://\S+\w|www\.\S+\w|@\w+|#\w+|<>&)~u',
array(__CLASS__, 'clickableCallback'),
html_entity_decode($s, ENT_QUOTES, 'UTF-8')
);
}



private static function clickableCallback($m)
{
$m = htmlspecialchars($m[1]);
if ($m[0] === '#') {
$m = substr($m, 1);
return "<a href='http://twitter.com/search?q=%23$m'>#$m</a>";
} elseif ($m[0] === '@') {
$m = substr($m, 1);
return "@<a href='http://www.twitter.com/$m'>$m</a>";
} elseif ($m[0] === 'w') {
return "<a href='http://$m'>$m</a>";
} elseif ($m[0] === 'h') {
return "<a href='$m'>$m</a>";
} else {
return $m;
}
}



/**
* Shortens URL using http://is.gd API.
* @param array
Expand Down

0 comments on commit 3fcb931

Please sign in to comment.