-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTwitterTextFormatter.php
336 lines (268 loc) · 8.2 KB
/
TwitterTextFormatter.php
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
namespace Netgloo;
/**
* Twitter Text Formatter : format twitter timeline text in html.
*
* @author Netgloo <info@netgloo.com>
*/
class TwitterTextFormatter {
// --------------------------------------------------------------------------
/**
* Return the tweet text formatted with the tweet's entities.
*
* Default configs:
*
* $configs = [
* 'show_retweeted_by' => true,
*
* 'retweeted_by_template' =>
* '<em> Retweeted by {{user_name}}</em>',
*
* 'hashtag_link_template' =>
* '<a href="{{hashtag_link}}" rel="nofollow" target="_blank">' .
* '#{{hashtag_text}}</a>',
*
* 'url_link_template' =>
* '<a href="{{url_link}}" rel="nofollow" target="_blank" ' .
* 'title="{{url_title}}">{{url_text}}</a>',
*
* 'user_mention_link_template' =>
* '<a href="{{user_mention_link}}" rel="nofollow" target="_blank" ' .
* title="{{user_mention_title}}">@{{user_mention_text}}</a>',
*
* 'media_link_template' =>
* '<a href="{{media_link}}" rel="nofollow" target="_blank" ' .
* 'title="{{media_title}}">{{media_text}}</a>'
* ];
*
* @param $tweet (Object)
* @param $configs (Array)
* @return (String)
*/
public static function format_text($tweet, $configs = []) {
// Set up configs
self::set_default(
$configs,
'show_retweeted_by',
true
);
self::set_default(
$configs,
'retweeted_by_template',
'<em> Retweeted by {{user_name}}</em>'
);
self::set_default(
$configs,
'hashtag_link_template',
'<a href="{{hashtag_link}}" rel="nofollow" target="_blank">' .
'#{{hashtag_text}}</a>'
);
self::set_default(
$configs,
'url_link_template',
'<a href="{{url_link}}" rel="nofollow" target="_blank" ' .
'title="{{url_title}}">{{url_text}}</a>'
);
self::set_default(
$configs,
'user_mention_link_template',
'<a href="{{user_mention_link}}" rel="nofollow" target="_blank" ' .
'title="{{user_mention_title}}">@{{user_mention_text}}</a>'
);
self::set_default(
$configs,
'media_link_template',
'<a href="{{media_link}}" rel="nofollow" target="_blank" ' .
'title="{{media_title}}">{{media_text}}</a>'
);
// Is retweeted?
if (isset($tweet->retweeted_status)) {
$user_name = $tweet->user->name;
$retweeted_by = '';
// If show retweeted by, then prepare the "retweeted by" text
if ($configs['show_retweeted_by']) {
$retweeted_by = $configs['retweeted_by_template'];
$retweeted_by = str_replace(
'{{user_name}}',
$user_name,
$retweeted_by
);
}
// Return the parsed re-tweet
$res = self::parse_tweet_text($tweet->retweeted_status, $configs);
return $res . $retweeted_by;
}
// Return the parsed tweet
return self::parse_tweet_text($tweet, $configs);
}
// --------------------------------------------------------------------------
// ==========================================================================
// PRIVATE METHODS
// --------------------------------------------------------------------------
/**
* Return the formatted text taking entities from the $tweet object.
*
* Credits: this function is a modified version of the one from Jacob
* Emerick's Blog (http://goo.gl/lhu8Ix)
*/
private static function parse_tweet_text($tweet, $configs) {
// Collects the set of entities
$entity_holder = array();
// Hashtags
if (isset($tweet->entities->hashtags)) {
$template = $configs['hashtag_link_template'];
foreach ($tweet->entities->hashtags as $hashtag) {
// Link: https://twitter.com/hashtag/{{1}}?src=hash
$hashtag_link = str_replace(
'{{1}}',
strtolower($hashtag->text),
'https://twitter.com/hashtag/{{1}}?src=hash'
);
$replace = str_replace(
'{{hashtag_link}}',
$hashtag_link,
$template
);
$replace = str_replace(
'{{hashtag_text}}',
$hashtag->text,
$replace
);
self::add_entity($entity_holder, $hashtag, $replace);
} // foreach
} // if
// Urls
if (isset($tweet->entities->urls)) {
$template = $configs['url_link_template'];
foreach ($tweet->entities->urls as $url) {
$replace = str_replace(
'{{url_link}}',
$url->url,
$template
);
$replace = str_replace(
'{{url_title}}',
$url->expanded_url,
$replace
);
$replace = str_replace(
'{{url_text}}',
$url->display_url,
$replace
);
self::add_entity($entity_holder, $url, $replace);
} // foreach
} // if
// User mentions
if (isset($tweet->entities->user_mentions)) {
$template = $configs['user_mention_link_template'];
foreach ($tweet->entities->user_mentions as $user_mention) {
// Link: https://twitter.com/{{1}}
$user_mention_link = str_replace(
'{{1}}',
strtolower($user_mention->screen_name),
'https://twitter.com/{{1}}'
);
$replace = str_replace(
'{{user_mention_link}}',
$user_mention_link,
$template
);
$replace = str_replace(
'{{user_mention_title}}',
$user_mention->name,
$replace
);
$replace = str_replace(
'{{user_mention_text}}',
$user_mention->screen_name,
$replace
);
self::add_entity($entity_holder, $user_mention, $replace);
} // foreach
} // if
// Media
if (isset($tweet->entities->media)) {
$template = $configs['media_link_template'];
foreach ($tweet->entities->media as $media) {
$replace = str_replace(
'{{media_link}}',
$media->url,
$template
);
$replace = str_replace(
'{{media_title}}',
$media->expanded_url,
$replace
);
$replace = str_replace(
'{{media_text}}',
$media->display_url,
$replace
);
self::add_entity($entity_holder, $media, $replace);
} // foreach
} // if
// Sort the entities in reverse order by their starting index
krsort($entity_holder);
// Replace the tweet's text with the entities
$text = $tweet->text;
foreach ($entity_holder as $entity) {
$text = self::mb_substr_replace(
$text,
$entity->replace,
$entity->start,
$entity->length,
'utf-8'
);
}
return $text;
}
// --------------------------------------------------------------------------
/**
* Add an entity to the entity_holder.
*/
private static function add_entity(
&$entity_holder,
$tweet_entity,
$replace
) {
$entity = new \stdClass();
$entity->start = $tweet_entity->indices[0];
$entity->end = $tweet_entity->indices[1];
$entity->length = $entity->end - $entity->start;
$entity->replace = $replace;
$entity_holder[$entity->start] = $entity;
return;
}
// --------------------------------------------------------------------------
/**
* String replacement supporting UTF-8 encoding.
*/
private static function mb_substr_replace(
$string,
$replacement,
$start,
$length = null,
$encoding = null
) {
$strlen = mb_strlen($string, $encoding);
$first_piece = mb_substr($string, 0, $start, $encoding) . $replacement;
$second_piece = '';
if (isset($length)) {
$second_piece = mb_substr($string, $start + $length, $strlen, $encoding);
}
return $first_piece . $second_piece;
}
// --------------------------------------------------------------------------
/**
* Set a default value for the given key.
*/
private static function set_default(&$array, $key, $default) {
if (!isset($array[$key])) {
$array[$key] = $default;
}
return;
}
// --------------------------------------------------------------------------
} // class