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
00032
00033 if (!class_exists('Flay')) {
00034 uses('flay');
00035 }
00036 if (!class_exists('HtmlHelper')) {
00037 uses('view' . DS . 'helpers' . DS . 'html');
00038 }
00039
00040
00041
00042
00043
00044
00045
00046
00047 class TextHelper extends Helper{
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 function highlight($text, $phrase, $highlighter = '<span class="highlight">\1</span>') {
00058 if (empty($phrase))
00059 return $text;
00060
00061 if (is_array($phrase)) {
00062 $replace=array();
00063 $with=array();
00064
00065 foreach ($phrase as $key => $value) {
00066 if (empty($key)) {
00067 $key =$value;
00068 $value=$highlighter;
00069 }
00070
00071 $replace[]='|(' . $key . ')|i';
00072 $with[]=empty($value) ? $highlighter : $value;
00073 }
00074
00075 return preg_replace($replace, $with, $text);
00076 } else {
00077 return preg_replace("|({$phrase})|i", $highlighter, $text);
00078 }
00079 }
00080
00081
00082
00083
00084
00085
00086
00087 function stripLinks($text) {
00088 return preg_replace('|<a.*>(.*)<\/a>|im', '\1', $text);
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 function autoLinkUrls($text, $htmlOptions = array()) {
00100 $options='array(';
00101
00102 foreach ($htmlOptions as $option => $value) {
00103 $options .= "'$option' => '$value', ";
00104 }
00105
00106 $options .= ')';
00107
00108 $text = preg_replace_callback('#(?<!href="|">)((?:http|https|ftp|nntp):
00109 create_function('$matches',
00110 '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $options . ');'),
00111 $text);
00112 return preg_replace_callback('#(?<!href="|">)(?<!http:
00113 create_function('$matches',
00114 '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . $matches[0],' . $options . ');'),
00115 $text);
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125 function autoLinkEmails($text, $htmlOptions = array()) {
00126 $options='array(';
00127
00128 foreach ($htmlOptions as $option => $value) {
00129 $options .= "'$option' => '$value', ";
00130 }
00131
00132 $options .= ')';
00133
00134 return preg_replace_callback(
00135 '#([_A-Za-z0-9+-]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#',
00136 create_function('$matches',
00137 '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->linkEmail($matches[0], $matches[0],' . $options . ');'),
00138 $text);
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148 function autoLink($text, $htmlOptions = array()) {
00149 return $this->autoLinkEmails($this->autoLinkUrls($text, $htmlOptions), $htmlOptions);
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 function truncate($text, $length, $ending = '...', $exact = true) {
00165 if (strlen($text) <= $length) {
00166 return $text;
00167 } else {
00168 $truncate=substr($text, 0, $length - strlen($ending));
00169
00170 if (!$exact) {
00171 $spacepos=strrpos($truncate, ' ');
00172
00173 if (isset($spacepos)) {
00174 return substr($truncate, 0, $spacepos) . $ending;
00175 }
00176 }
00177
00178 return $truncate . $ending;
00179 }
00180 }
00181
00182
00183
00184
00185
00186
00187
00188 function trim() {
00189 $args=func_get_args();
00190 return call_user_func_array(array(&$this,
00191 "truncate"), $args);
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 function excerpt($text, $phrase, $radius = 100, $ending = "...") {
00204 if (empty($text) or empty($phrase))
00205 return $this->truncate($text, $radius * 2, $ending);
00206
00207 if ($radius < strlen($phrase))
00208 $radius=strlen($phrase);
00209
00210 $pos =strpos($text, $phrase);
00211 $startPos=$pos <= $radius ? 0 : $pos - $radius;
00212 $endPos =$pos + strlen($phrase) + $radius >= strlen($text)
00213 ? strlen($text) : $pos + strlen($phrase) + $radius;
00214
00215 $excerpt =substr($text, $startPos, $endPos - $startPos);
00216
00217 if ($startPos != 0)
00218 $excerpt=substr_replace($excerpt, $ending, 0, strlen($phrase));
00219
00220 if ($endPos != strlen($text))
00221 $excerpt=substr_replace($excerpt, $ending, -strlen($phrase));
00222
00223 return $excerpt;
00224 }
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 function flay($text, $allowHtml = false) {
00235 return Flay::toHtml($text, false, $allowHtml);
00236 }
00237 }
00238 ?>