Skip to content
Browse files

clickable() uses entities, if available [Closes #9]

  • Loading branch information...
1 parent 25d1ae8 commit 0b3d86c2c8638f4c2c2f1b5f913a372ad3f6bf65 @dg committed
Showing with 20 additions and 14 deletions.
  1. +1 −1 examples/custom-request.php
  2. +1 −1 examples/load.php
  3. +1 −1 examples/search.php
  4. +17 −11 src/twitter.class.php
View
2 examples/custom-request.php
@@ -17,7 +17,7 @@
<?php foreach ($statuses 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 Twitter::clickable($status->text) ?>
+ <?php echo Twitter::clickable($status) ?>
<small>at <?php echo date("j.n.Y H:i", strtotime($status->created_at)) ?></small>
</li>
<?php endforeach ?>
View
2 examples/load.php
@@ -20,7 +20,7 @@
<?php foreach ($statuses 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 Twitter::clickable($status->text) ?>
+ <?php echo Twitter::clickable($status) ?>
<small>at <?php echo date("j.n.Y H:i", strtotime($status->created_at)) ?></small>
</li>
<?php endforeach ?>
View
2 examples/search.php
@@ -18,7 +18,7 @@
<?php foreach ($results 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 Twitter::clickable($status->text) ?>
+ <?php echo Twitter::clickable($status) ?>
<small>at <?php echo date("j.n.Y H:i", strtotime($status->created_at)) ?></small>
</li>
<?php endforeach ?>
View
28 src/twitter.class.php
@@ -264,31 +264,37 @@ public function cachedRequest($resource, array $data = NULL, $cacheExpire = NULL
*/
public static function clickable($status)
{
+ if (!is_object($status)) { // back compatibility
+ return preg_replace_callback(
+ '~(?<!\w)(https?://\S+\w|www\.\S+\w|@\w+|#\w+)|[<>&]~u',
+ array(__CLASS__, 'clickableCallback'),
+ html_entity_decode($status, ENT_QUOTES, 'UTF-8')
+ );
+ }
+
$all = array();
foreach ($status->entities->hashtags as $item) {
- $all[$item->indices[0]] = array("<a href='http://twitter.com/search?q=%23$item->text'>#$item->text</a>", $item->indices[1]);
+ $all[$item->indices[0]] = array("http://twitter.com/search?q=%23$item->text", "#$item->text", $item->indices[1]);
}
foreach ($status->entities->urls as $item) {
if (!isset($item->expanded_url)) {
- $item->expanded_url = $item->display_url = $item->url;
+ $all[$item->indices[0]] = array($item->url, $item->url, $item->indices[1]);
+ } else {
+ $all[$item->indices[0]] = array($item->expanded_url, $item->display_url, $item->indices[1]);
}
- $all[$item->indices[0]] = array("<a href='$item->expanded_url'>$item->display_url</a>", $item->indices[1]);
}
foreach ($status->entities->user_mentions as $item) {
- $all[$item->indices[0]] = array("<a href='http://twitter.com/$item->screen_name'>@$item->screen_name</a>", $item->indices[1]);
+ $all[$item->indices[0]] = array("http://twitter.com/$item->screen_name", "@$item->screen_name", $item->indices[1]);
}
+
krsort($all);
$s = $status->text;
foreach ($all as $pos => $item) {
- $s = iconv_substr($s, 0, $pos, 'UTF-8') . $item[0] . iconv_substr($s, $item[2], iconv_strlen($s, 'UTF-8'), 'UTF-8');
+ $s = iconv_substr($s, 0, $pos, 'UTF-8')
+ . '<a href="' . htmlspecialchars($item[0]) . '">' . htmlspecialchars($item[1]) . '</a>'
+ . iconv_substr($s, $item[2], iconv_strlen($s, 'UTF-8'), 'UTF-8');
}
return $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')
- );
}

0 comments on commit 0b3d86c

Please sign in to comment.
Something went wrong with that request. Please try again.