00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 if (!class_exists('HtmlHelper')) {
00032 App::import('Helper', 'Html');
00033 }
00034 if (!class_exists('Multibyte')) {
00035 App::import('Core', 'Multibyte');
00036 }
00037
00038
00039
00040
00041
00042
00043
00044
00045 class TextHelper extends AppHelper {
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 function highlight($text, $phrase, $highlighter = '<span class="highlight">\1</span>', $considerHtml = false) {
00058 if (empty($phrase)) {
00059 return $text;
00060 }
00061
00062 if (is_array($phrase)) {
00063 $replace = array();
00064 $with = array();
00065
00066 foreach ($phrase as $key => $value) {
00067 $key = $value;
00068 $value = $highlighter;
00069 $key = '(' . $key . ')';
00070 if ($considerHtml) {
00071 $key = '(?![^<]+>)' . $key . '(?![^<]+>)';
00072 }
00073 $replace[] = '|' . $key . '|iu';
00074 $with[] = empty($value) ? $highlighter : $value;
00075 }
00076
00077 return preg_replace($replace, $with, $text);
00078 } else {
00079 $phrase = '(' . $phrase . ')';
00080 if ($considerHtml) {
00081 $phrase = '(?![^<]+>)' . $phrase . '(?![^<]+>)';
00082 }
00083
00084 return preg_replace('|'.$phrase.'|iu', $highlighter, $text);
00085 }
00086 }
00087
00088
00089
00090
00091
00092
00093
00094 function stripLinks($text) {
00095 return preg_replace('|<a\s+[^>]+>|im', '', preg_replace('|<\/a>|im', '', $text));
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 function autoLinkUrls($text, $htmlOptions = array()) {
00107 $options = 'array(';
00108 foreach ($htmlOptions as $option => $value) {
00109 $value = var_export($value, true);
00110 $options .= "'$option' => $value, ";
00111 }
00112 $options .= ')';
00113
00114 $text = preg_replace_callback('#(?<!href="|">)((?:http|https|ftp|nntp):
00115 '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $options . ');'), $text);
00116
00117 return preg_replace_callback('#(?<!href="|">)(?<!http:
00118 create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . strtolower($matches[0]),' . $options . ');'), $text);
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128 function autoLinkEmails($text, $htmlOptions = array()) {
00129 $options = 'array(';
00130
00131 foreach ($htmlOptions as $option => $value) {
00132 $options .= "'$option' => '$value', ";
00133 }
00134 $options .= ')';
00135
00136 return preg_replace_callback('#([_A-Za-z0-9+-]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#',
00137 create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "mailto:" . $matches[0],' . $options . ');'), $text);
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147 function autoLink($text, $htmlOptions = array()) {
00148 return $this->autoLinkEmails($this->autoLinkUrls($text, $htmlOptions), $htmlOptions);
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 function truncate($text, $length = 100, $ending = '...', $exact = true, $considerHtml = false) {
00164 if (is_array($ending)) {
00165 extract($ending);
00166 }
00167 if ($considerHtml) {
00168 if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
00169 return $text;
00170 }
00171 $totalLength = mb_strlen($ending);
00172 $openTags = array();
00173 $truncate = '';
00174 preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
00175 foreach ($tags as $tag) {
00176 if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) {
00177 if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) {
00178 array_unshift($openTags, $tag[2]);
00179 } else if (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) {
00180 $pos = array_search($closeTag[1], $openTags);
00181 if ($pos !== false) {
00182 array_splice($openTags, $pos, 1);
00183 }
00184 }
00185 }
00186 $truncate .= $tag[1];
00187
00188 $contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3]));
00189 if ($contentLength + $totalLength > $length) {
00190 $left = $length - $totalLength;
00191 $entitiesLength = 0;
00192 if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE)) {
00193 foreach ($entities[0] as $entity) {
00194 if ($entity[1] + 1 - $entitiesLength <= $left) {
00195 $left--;
00196 $entitiesLength += mb_strlen($entity[0]);
00197 } else {
00198 break;
00199 }
00200 }
00201 }
00202
00203 $truncate .= mb_substr($tag[3], 0 , $left + $entitiesLength);
00204 break;
00205 } else {
00206 $truncate .= $tag[3];
00207 $totalLength += $contentLength;
00208 }
00209 if ($totalLength >= $length) {
00210 break;
00211 }
00212 }
00213
00214 } else {
00215 if (mb_strlen($text) <= $length) {
00216 return $text;
00217 } else {
00218 $truncate = mb_substr($text, 0, $length - strlen($ending));
00219 }
00220 }
00221 if (!$exact) {
00222 $spacepos = mb_strrpos($truncate, ' ');
00223 if (isset($spacepos)) {
00224 if ($considerHtml) {
00225 $bits = mb_substr($truncate, $spacepos);
00226 preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER);
00227 if (!empty($droppedTags)) {
00228 foreach ($droppedTags as $closingTag) {
00229 if (!in_array($closingTag[1], $openTags)) {
00230 array_unshift($openTags, $closingTag[1]);
00231 }
00232 }
00233 }
00234 }
00235 $truncate = mb_substr($truncate, 0, $spacepos);
00236 }
00237 }
00238
00239 $truncate .= $ending;
00240
00241 if ($considerHtml) {
00242 foreach ($openTags as $tag) {
00243 $truncate .= '</'.$tag.'>';
00244 }
00245 }
00246
00247 return $truncate;
00248 }
00249
00250
00251
00252
00253
00254
00255 function trim() {
00256 $args = func_get_args();
00257 return call_user_func_array(array(&$this, 'truncate'), $args);
00258 }
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 function excerpt($text, $phrase, $radius = 100, $ending = "...") {
00270 if (empty($text) or empty($phrase)) {
00271 return $this->truncate($text, $radius * 2, $ending);
00272 }
00273
00274 $phraseLen = strlen($phrase);
00275 if ($radius < $phraseLen) {
00276 $radius = $phraseLen;
00277 }
00278
00279 $pos = strpos(strtolower($text), strtolower($phrase));
00280
00281 $startPos = 0;
00282 if ($pos > $radius) {
00283 $startPos = $pos - $radius;
00284 }
00285
00286 $textLen = strlen($text);
00287
00288 $endPos = $pos + $phraseLen + $radius;
00289 if ($endPos >= $textLen) {
00290 $endPos = $textLen;
00291 }
00292
00293 $excerpt = substr($text, $startPos, $endPos - $startPos);
00294 if ($startPos != 0) {
00295 $excerpt = substr_replace($excerpt, $ending, 0, $phraseLen);
00296 }
00297
00298 if ($endPos != $textLen) {
00299 $excerpt = substr_replace($excerpt, $ending, -$phraseLen);
00300 }
00301
00302 return $excerpt;
00303 }
00304
00305
00306
00307
00308
00309
00310
00311 function toList($list, $and = 'and') {
00312 $r = '';
00313 $c = count($list) - 1;
00314 foreach ($list as $i => $item) {
00315 $r .= $item;
00316 if ($c > 0 && $i < $c)
00317 {
00318 $r .= ($i < $c - 1 ? ', ' : " {$and} ");
00319 }
00320 }
00321 return $r;
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333 function flay($text, $allowHtml = false) {
00334 trigger_error(__('(TextHelper::flay) Deprecated: the Flay library is no longer supported and will be removed in a future version.', true), E_USER_WARNING);
00335 if (!class_exists('Flay')) {
00336 uses('flay');
00337 }
00338 return Flay::toHtml($text, false, $allowHtml);
00339 }
00340
00341
00342
00343 }
00344 ?>