This repository has been archived by the owner on Apr 12, 2019. It is now read-only.
/
example.php
229 lines (206 loc) · 5.58 KB
/
example.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
<?php
/**
* Examples for the Twitter Text (PHP Edition) Library.
*
* Can be run on command line or in the browser.
*
* @author Mike Cochrane <mikec@mikenz.geek.nz>
* @author Nick Pope <nick@nickpope.me.uk>
* @copyright Copyright © 2010, Mike Cochrane, Nick Pope
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
*/
if (!defined('E_DEPRECATED')) define('E_DEPRECATED', 8192);
error_reporting(E_ALL | E_STRICT | E_DEPRECATED);
$ROOT = dirname(dirname(__FILE__));
require_once $ROOT.'/lib/Twitter/Autolink.php';
require_once $ROOT.'/lib/Twitter/Extractor.php';
require_once $ROOT.'/lib/Twitter/HitHighlighter.php';
$browser = (PHP_SAPI != 'cli');
function print_array(array $a) {
$p = print_r($a, true);
$p = str_replace(' ', ' ', $p);
echo preg_replace(array(
'!^Array\s+\(\s+!',
'!=> Array\s+\(!',
'! (\[\d|\))!',
'!\s+\)\s*$!',
), array(
' ', '=> (', '\1', '',
), $p);
}
$tweet = 'Tweet mentioning @mikenz and referring to his list @mikeNZ/sports and website http://mikenz.geek.nz #awesome';
if ($browser) echo <<<EOHTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en-GB" xml:lang="en-GB" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Twitter Text (PHP Edition) Library » Examples</title>
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
body {
font-family: Arial, sans-serif;
font-size: 12px;
}
.source, .markup, .output {
margin-left: 20px;
width: 900px;
-moz-border-radius: 8px;
-khtml-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
}
.source {
background: #eee;
border: solid 1px #ddd;
padding: 0 20px;
}
.markup {
background: #efe;
border: solid 1px #ded;
padding: 20px;
white-space: pre-wrap;
}
.output {
background: #fed;
border: solid 1px #edc;
padding: 20px;
}
/*]]>*/-->
</style>
</head>
<body>
EOHTML;
if ($browser) echo '<h1>';
echo 'Twitter Text (PHP Edition) Library » Examples';
if ($browser) echo '</h1>';
else echo PHP_EOL, '============================================', PHP_EOL;
echo PHP_EOL;
if ($browser) echo '<h2>';
echo 'Extraction Examples';
if ($browser) echo '</h2>';
else echo PHP_EOL, '-------------------', PHP_EOL;
echo PHP_EOL;
$code = <<<EOPHP
<?php
\$tweet = 'Tweet mentioning @mikenz and referring to his list @mikeNZ/sports and website http://mikenz.geek.nz #awesome';
\$data = Twitter_Extractor::create(\$tweet)
->extract();
print_r(\$data);
EOPHP;
if ($browser) {
echo '<h3>Source</h3>', PHP_EOL;
echo '<pre class="source">';
highlight_string($code);
echo '</pre>', PHP_EOL;
} else {
echo 'Source:', PHP_EOL, PHP_EOL;
echo $code;
echo PHP_EOL, PHP_EOL;
}
$data = Twitter_Extractor::create($tweet)
->extract();
if ($browser) {
echo '<h3>Output</h3>', PHP_EOL;
echo '<pre class="output">';
print_array($data);
echo '</pre>', PHP_EOL;
} else {
echo 'Output:', PHP_EOL, PHP_EOL;
print_array($data);
echo PHP_EOL, PHP_EOL;
}
if ($browser) echo '<h2>';
echo 'Autolink Examples';
if ($browser) echo '</h2>';
else echo PHP_EOL, '-----------------', PHP_EOL;
echo PHP_EOL;
$code = <<<EOPHP
<?php
\$tweet = 'Tweet mentioning @mikenz and referring to his list @mikeNZ/sports and website http://mikenz.geek.nz #awesome';
\$html = Twitter_Autolink::create(\$tweet)
->setNoFollow(false)
->addLinks();
echo \$html;
EOPHP;
if ($browser) {
echo '<h3>Source</h3>', PHP_EOL;
echo '<pre class="source">';
highlight_string($code);
echo '</pre>', PHP_EOL;
} else {
echo 'Source:', PHP_EOL, PHP_EOL;
echo $code;
echo PHP_EOL, PHP_EOL;
}
$html = Twitter_Autolink::create($tweet)
->setNoFollow(false)
->addLinks();
if ($browser) {
echo '<h3>Markup</h3>', PHP_EOL;
echo '<pre class="markup"><code>';
echo htmlspecialchars($html, ENT_QUOTES, 'UTF-8', false);
echo '</code></pre>', PHP_EOL;
} else {
echo 'Markup:', PHP_EOL, PHP_EOL;
echo wordwrap(htmlspecialchars($html, ENT_QUOTES, 'UTF-8', false));
echo PHP_EOL, PHP_EOL;
}
if ($browser) {
echo '<h3>Output</h3>', PHP_EOL;
echo '<div class="output">';
echo $html;
echo '</div>', PHP_EOL;
} else {
echo 'Output:', PHP_EOL, PHP_EOL;
echo wordwrap($html);
echo PHP_EOL, PHP_EOL;
}
if ($browser) echo '<h2>';
echo 'Hit Highlighter Examples';
if ($browser) echo '</h2>';
else echo PHP_EOL, '------------------------', PHP_EOL;
echo PHP_EOL;
$code = <<<EOPHP
<?php
\$tweet = 'Tweet mentioning @mikenz and referring to his list @mikeNZ/sports and website http://mikenz.geek.nz #awesome';
\$hits = array(array(70, 77), array(101, 108));
\$html = Twitter_HitHighlighter::create(\$tweet)
->addHitHighlighting(\$hits);
echo \$html;
EOPHP;
if ($browser) {
echo '<h3>Source</h3>', PHP_EOL;
echo '<pre class="source">';
highlight_string($code);
echo '</pre>', PHP_EOL;
} else {
echo 'Source:', PHP_EOL, PHP_EOL;
echo $code;
echo PHP_EOL, PHP_EOL;
}
$html = Twitter_HitHighlighter::create($tweet)
->addHitHighlighting(array(array(70, 77), array(101, 108)));
if ($browser) {
echo '<h3>Markup</h3>', PHP_EOL;
echo '<pre class="markup"><code>';
echo htmlspecialchars($html, ENT_QUOTES, 'UTF-8', false);
echo '</code></pre>', PHP_EOL;
} else {
echo 'Markup:', PHP_EOL, PHP_EOL;
echo wordwrap(htmlspecialchars($html, ENT_QUOTES, 'UTF-8', false));
echo PHP_EOL, PHP_EOL;
}
if ($browser) {
echo '<h3>Output</h3>', PHP_EOL;
echo '<div class="output">';
echo $html;
echo '</div>', PHP_EOL;
} else {
echo 'Output:', PHP_EOL, PHP_EOL;
echo wordwrap($html);
echo PHP_EOL, PHP_EOL;
}
if ($browser) echo <<<EOHTML
</body>
</html>
EOHTML;