1: <?php
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: if (!class_exists('Object')) {
31: uses('object');
32: }
33: 34: 35: 36: 37: 38: 39: 40:
41: class Flay extends Object{
42: 43: 44: 45: 46: 47:
48: var $text = null;
49: 50: 51: 52: 53: 54:
55: var $allow_html = false;
56: 57: 58: 59: 60:
61: function __construct($text = null) {
62: $this->text = $text;
63: parent::__construct();
64: }
65: 66: 67: 68: 69: 70: 71: 72: 73:
74: function toHtml($text = null, $bare = false, $allowHtml = false) {
75: if (empty($text) && empty($this->text)) {
76: return false;
77: }
78: $text = $text ? $text : $this->text;
79:
80: if ($allowHtml) {
81: $text = trim($text);
82: } else {
83: $text = str_replace('<', '<', str_replace('>', '>', trim($text)));
84: }
85:
86: if (!$bare) {
87:
88: $text=preg_replace('#(?:[\n]{0,2})"""(.*)"""(?:[\n]{0,2})#s', "\n\n%BLOCKQUOTE%\n\n\\1\n\n%ENDBLOCKQUOTE%\n\n", $text);
89: $text=preg_replace('#(?:[\n]{0,2})===(.*)===(?:[\n]{0,2})#s', "\n\n%CENTER%\n\n\\1\n\n%ENDCENTER%\n\n", $text);
90: }
91:
92:
93: $text=preg_replace("#\r\n#", "\n", $text);
94: $text=preg_replace("#[\n]{2,}#", "%PARAGRAPH%", $text);
95: $text=preg_replace('#[\n]{1}#', "%LINEBREAK%", $text);
96: $out ='';
97:
98: foreach (split('%PARAGRAPH%', $text)as $line) {
99: if ($line) {
100: if (!$bare) {
101: $links = array();
102: $regs = null;
103:
104: if (preg_match_all('#\[([^\[]{4,})\]#', $line, $regs)) {
105: foreach ($regs[1] as $reg) {
106: $links[] = $reg;
107: $line = str_replace("[{$reg}]", '%LINK' . (count($links) - 1) . '%', $line);
108: }
109: }
110:
111: $line = ereg_replace("\*([^\*]*)\*", "<strong>\\1</strong>", $line);
112:
113: $line = ereg_replace("_([^_]*)_", "<em>\\1</em>", $line);
114: }
115:
116: $line = str_replace(' - ', ' – ', $line);
117: $line = str_replace(' -- ', ' — ', $line);
118: $line = str_replace('(C)', '©', $line);
119: $line = str_replace('(R)', '®', $line);
120: $line = str_replace('(TM)', '™', $line);
121:
122: $emails = null;
123: if (preg_match_all("#([_A-Za-z0-9+-+]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#", $line, $emails)) {
124: foreach ($emails[1] as $email) {
125: $line = str_replace($email, "<a href=\"mailto:{$email}\">{$email}</a>", $line);
126: }
127: }
128:
129: if (!$bare) {
130: $urls = null;
131: if (preg_match_all("#((?:http|https|ftp|nntp)://[^ ]+)#", $line, $urls)) {
132: foreach ($urls[1] as $url) {
133: $line = str_replace($url, "<a href=\"{$url}\">{$url}</a>", $line);
134: }
135: }
136:
137: if (preg_match_all("#(www\.[^\n\%\ ]+[^\n\%\,\.\ ])#", $line, $urls)) {
138: foreach ($urls[1] as $url) {
139: $line = str_replace($url, "<a href=\"http://{$url}\">{$url}</a>", $line);
140: }
141: }
142:
143: if ($count = count($links)) {
144: for ($ii = 0; $ii < $count; $ii++) {
145: if (preg_match("#^(http|https|ftp|nntp)://#", $links[$ii])) {
146: $prefix = null;
147: } else {
148: $prefix = 'http://';
149: }
150: if (preg_match('#^[^\ ]+\.(jpg|jpeg|gif|png)$#', $links[$ii])) {
151: $with = "<img src=\"{$prefix}{$links[$ii]}\" alt=\"\" />";
152: } elseif (preg_match('#^([^\]\ ]+)(?:\ ([^\]]+))?$#', $links[$ii], $regs)) {
153: if (isset($regs[2])) {
154: if (preg_match('#\.(jpg|jpeg|gif|png)$#', $regs[2])) {
155: $body = "<img src=\"{$prefix}{$regs[2]}\" alt=\"\" />";
156: } else {
157: $body = $regs[2];
158: }
159: } else {
160: $body = $links[$ii];
161: }
162: $with = "<a href=\"{$prefix}{$regs[1]}\" target=\"_blank\">{$body}</a>";
163: } else {
164: $with = $prefix . $links[$ii];
165: }
166: $line = str_replace("%LINK{$ii}%", $with, $line);
167: }
168: }
169: }
170: $out .= str_replace('%LINEBREAK%', "<br />\n", "<p>{$line}</p>\n");
171: }
172: }
173:
174: if (!$bare) {
175: $out = str_replace('<p>%BLOCKQUOTE%</p>', "<blockquote>", $out);
176: $out = str_replace('<p>%ENDBLOCKQUOTE%</p>', "</blockquote>", $out);
177: $out = str_replace('<p>%CENTER%</p>', "<center>", $out);
178: $out = str_replace('<p>%ENDCENTER%</p>', "</center>", $out);
179: }
180: return $out;
181: }
182: 183: 184: 185: 186: 187: 188:
189: function extractWords($string) {
190: $split = preg_split('/[\s,\.:\/="!\(\)<>~\[\]]+/', $string);
191: return $split;
192: }
193: 194: 195: 196: 197: 198: 199: 200: 201: 202:
203: function markedSnippets($words, $string, $max_snippets = 5) {
204: $string = strip_tags($string);
205: $snips = array();
206: $rest = $string;
207: foreach ($words as $word) {
208: if (preg_match_all("/[\s,]+.{0,40}{$word}.{0,40}[\s,]+/i", $rest, $r)) {
209: foreach ($r as $result) {
210: $rest = str_replace($result, '', $rest);
211: }
212: $snips = array_merge($snips, $r[0]);
213: }
214: }
215:
216: if (count($snips) > $max_snippets) {
217: $snips = array_slice($snips, 0, $max_snippets);
218: }
219: $joined = implode(' <b>...</b> ', $snips);
220: $snips = $joined ? "<b>...</b> {$joined} <b>...</b>" : substr($string, 0, 80) . '<b>...</b>';
221: return $this->colorMark($words, $snips);
222: }
223: 224: 225: 226: 227: 228: 229: 230:
231: function colorMark($words, $string) {
232: $colors=array('yl', 'gr', 'rd', 'bl', 'fu', 'cy');
233: $nextColorIndex = 0;
234: foreach ($words as $word) {
235: $string = preg_replace("/({$word})/i", '<em class="' . $colors[$nextColorIndex % count($colors)] . "\">\\1</em>", $string);
236: $nextColorIndex++;
237: }
238: return $string;
239: }
240: 241: 242: 243: 244: 245: 246:
247: function toClean($text) {
248: $strip = strip_tags(html_entity_decode($text, ENT_QUOTES));
249: return $strip;
250: }
251: 252: 253: 254: 255: 256: 257:
258: function toParsedAndClean($text) {
259: return $this->toClean(Flay::toHtml($text));
260: }
261: 262: 263: 264: 265: 266: 267: 268: 269:
270: function fragment($text, $length, $ellipsis = '...') {
271: $soft = $length - 5;
272: $hard = $length + 5;
273: $rx = '/(.{' . $soft . ',' . $hard . '})[\s,\.:\/="!\(\)<>~\[\]]+.*/';
274:
275: if (preg_match($rx, $text, $r)) {
276: $out = $r[1];
277: } else {
278: $out = substr($text, 0, $length);
279: }
280: $out = $out . (strlen($out) < strlen($text) ? $ellipsis : null);
281: return $out;
282: }
283: }
284: ?>