-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtweetify.inc
56 lines (49 loc) · 1.33 KB
/
tweetify.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/*
* tweetify.inc
*
* Ported from Remy Sharp's 'ify' javascript function; see:
* http://code.google.com/p/twitterjs/source/browse/trunk/src/ify.js
*
* Based on revision 46:
* http://code.google.com/p/twitterjs/source/detail?spec=svn46&r=46
*/
/*
* Clean a tweet: translate links, usernames beginning '@', and hashtags
*/
function clean_tweet($tweet)
{
$regexps = array
(
"link" => '/[a-z]+:\/\/[a-z0-9-_]+\.[a-z0-9-_@:~%&\?\+#\/.=]+[^:\.,\)\s*$]/i',
"at" => '/(^|[^\w]+)\@([a-zA-Z0-9_]{1,15}(\/[a-zA-Z0-9-_]+)*)/',
"hash" => "/(^|[^&\w'\"]+)\#([a-zA-Z0-9_]+)/"
);
foreach ($regexps as $name => $re)
{
$tweet = preg_replace_callback($re, 'parse_tweet_'.$name, $tweet);
}
return $tweet;
}
/*
* Wrap a link element around URLs matched via preg_replace()
*/
function parse_tweet_link($m)
{
return '<a href="'.$m[0].'">'.((strlen($m[0]) > 25) ? substr($m[0], 0, 24).'...' : $m[0]).'</a>';
}
/*
* Wrap a link element around usernames matched via preg_replace()
*/
function parse_tweet_at($m)
{
return $m[1].'@<a href="http://twitter.com/'.$m[2].'">'.$m[2].'</a>';
}
/*
* Wrap a link element around hashtags matched via preg_replace()
*/
function parse_tweet_hash($m)
{
return $m[1].'#<a href="http://search.twitter.com/search?q=%23'.$m[2].'">'.$m[2].'</a>';
}
?>