| 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 |
function truncate_text($text, $length = 30, $truncate_string = '...', $truncate_lastspace = false) |
|---|
| 27 |
{ |
|---|
| 28 |
if ($text == '') |
|---|
| 29 |
{ |
|---|
| 30 |
return ''; |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
$mbstring = extension_loaded('mbstring'); |
|---|
| 34 |
if($mbstring) |
|---|
| 35 |
{ |
|---|
| 36 |
$old_encoding = mb_internal_encoding(); |
|---|
| 37 |
@mb_internal_encoding(mb_detect_encoding($text)); |
|---|
| 38 |
} |
|---|
| 39 |
$strlen = ($mbstring) ? 'mb_strlen' : 'strlen'; |
|---|
| 40 |
$substr = ($mbstring) ? 'mb_substr' : 'substr'; |
|---|
| 41 |
|
|---|
| 42 |
if ($strlen($text) > $length) |
|---|
| 43 |
{ |
|---|
| 44 |
$truncate_text = $substr($text, 0, $length - $strlen($truncate_string)); |
|---|
| 45 |
if ($truncate_lastspace) |
|---|
| 46 |
{ |
|---|
| 47 |
$truncate_text = preg_replace('/\s+?(\S+)?$/', '', $truncate_text); |
|---|
| 48 |
} |
|---|
| 49 |
$text = $truncate_text.$truncate_string; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
if($mbstring) |
|---|
| 53 |
{ |
|---|
| 54 |
@mb_internal_encoding($old_encoding); |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
return $text; |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
function highlight_text($text, $phrase, $highlighter = '<strong class="highlight">\\1</strong>') |
|---|
| 73 |
{ |
|---|
| 74 |
if (empty($text)) |
|---|
| 75 |
{ |
|---|
| 76 |
return ''; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
if (empty($phrase)) |
|---|
| 80 |
{ |
|---|
| 81 |
return $text; |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
if (is_array($phrase) or ($phrase instanceof sfOutputEscaperArrayDecorator)) |
|---|
| 85 |
{ |
|---|
| 86 |
foreach ($phrase as $word) |
|---|
| 87 |
{ |
|---|
| 88 |
$pattern[] = '/('.preg_quote($word, '/').')/i'; |
|---|
| 89 |
$replacement[] = $highlighter; |
|---|
| 90 |
} |
|---|
| 91 |
} |
|---|
| 92 |
else |
|---|
| 93 |
{ |
|---|
| 94 |
$pattern = '/('.preg_quote($phrase, '/').')/i'; |
|---|
| 95 |
$replacement = $highlighter; |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
return preg_replace($pattern, $replacement, $text); |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
function excerpt_text($text, $phrase, $radius = 100, $excerpt_string = '...', $excerpt_space = false) |
|---|
| 110 |
{ |
|---|
| 111 |
if ($text == '' || $phrase == '') |
|---|
| 112 |
{ |
|---|
| 113 |
return ''; |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
$mbstring = extension_loaded('mbstring'); |
|---|
| 117 |
if($mbstring) |
|---|
| 118 |
{ |
|---|
| 119 |
$old_encoding = mb_internal_encoding(); |
|---|
| 120 |
@mb_internal_encoding(mb_detect_encoding($text)); |
|---|
| 121 |
} |
|---|
| 122 |
$strlen = ($mbstring) ? 'mb_strlen' : 'strlen'; |
|---|
| 123 |
$strpos = ($mbstring) ? 'mb_strpos' : 'strpos'; |
|---|
| 124 |
$strtolower = ($mbstring) ? 'mb_strtolower' : 'strtolower'; |
|---|
| 125 |
$substr = ($mbstring) ? 'mb_substr' : 'substr'; |
|---|
| 126 |
|
|---|
| 127 |
$found_pos = $strpos($strtolower($text), $strtolower($phrase)); |
|---|
| 128 |
$return_string = ''; |
|---|
| 129 |
if ($found_pos !== false) |
|---|
| 130 |
{ |
|---|
| 131 |
$start_pos = max($found_pos - $radius, 0); |
|---|
| 132 |
$end_pos = min($found_pos + $strlen($phrase) + $radius, $strlen($text)); |
|---|
| 133 |
$excerpt = $substr($text, $start_pos, $end_pos - $start_pos); |
|---|
| 134 |
$prefix = ($start_pos > 0) ? $excerpt_string : ''; |
|---|
| 135 |
$postfix = $end_pos < $strlen($text) ? $excerpt_string : ''; |
|---|
| 136 |
|
|---|
| 137 |
if ($excerpt_space) |
|---|
| 138 |
{ |
|---|
| 139 |
|
|---|
| 140 |
if($prefix) |
|---|
| 141 |
{ |
|---|
| 142 |
$excerpt = preg_replace('/^(\S+)?\s+?/', ' ', $excerpt); |
|---|
| 143 |
} |
|---|
| 144 |
if($postfix) |
|---|
| 145 |
{ |
|---|
| 146 |
$excerpt = preg_replace('/\s+?(\S+)?$/', ' ', $excerpt); |
|---|
| 147 |
} |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
$return_string = $prefix.$excerpt.$postfix; |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
if($mbstring) |
|---|
| 154 |
{ |
|---|
| 155 |
@mb_internal_encoding($old_encoding); |
|---|
| 156 |
} |
|---|
| 157 |
return $return_string; |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
function wrap_text($text, $line_width = 80) |
|---|
| 164 |
{ |
|---|
| 165 |
return preg_replace('/(.{1,'.$line_width.'})(\s+|$)/s', "\\1\n", preg_replace("/\n/", "\n\n", $text)); |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
function simple_format_text($text, $options = array()) |
|---|
| 175 |
{ |
|---|
| 176 |
$css = (isset($options['class'])) ? ' class="'.$options['class'].'"' : ''; |
|---|
| 177 |
|
|---|
| 178 |
$text = sfToolkit::pregtr($text, array("/(\r\n|\r)/" => "\n", |
|---|
| 179 |
"/\n{2,}/" => "</p><p$css>")); |
|---|
| 180 |
|
|---|
| 181 |
// turn single newline into <br/> |
|---|
| 182 |
$text = str_replace("\n", "\n<br />", $text); |
|---|
| 183 |
return '<p'.$css.'>'.$text.'</p>'; |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
function auto_link_text($text, $link = 'all', $href_options = array(), $truncate = false, $truncate_len = 35, $pad = '...') |
|---|
| 196 |
{ |
|---|
| 197 |
if ($link == 'all') |
|---|
| 198 |
{ |
|---|
| 199 |
return _auto_link_urls(_auto_link_email_addresses($text), $href_options, $truncate, $truncate_len, $pad); |
|---|
| 200 |
} |
|---|
| 201 |
else if ($link == 'email_addresses') |
|---|
| 202 |
{ |
|---|
| 203 |
return _auto_link_email_addresses($text); |
|---|
| 204 |
} |
|---|
| 205 |
else if ($link == 'urls') |
|---|
| 206 |
{ |
|---|
| 207 |
return _auto_link_urls($text, $href_options, $truncate, $truncate_len, $pad); |
|---|
| 208 |
} |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
function strip_links_text($text) |
|---|
| 215 |
{ |
|---|
| 216 |
return preg_replace('/<a[^>]*>(.*?)<\/a>/s', '\\1', $text); |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
if (!defined('SF_AUTO_LINK_RE')) |
|---|
| 220 |
{ |
|---|
| 221 |
define('SF_AUTO_LINK_RE', '~ |
|---|
| 222 |
( # leading text |
|---|
| 223 |
<\w+.*?>| # leading HTML tag, or |
|---|
| 224 |
[^=!:\'"/]| # leading punctuation, or |
|---|
| 225 |
^ # beginning of line |
|---|
| 226 |
) |
|---|
| 227 |
( |
|---|
| 228 |
(?:https?://)| # protocol spec, or |
|---|
| 229 |
(?:www\.) # www.* |
|---|
| 230 |
) |
|---|
| 231 |
( |
|---|
| 232 |
[-\w]+ # subdomain or domain |
|---|
| 233 |
(?:\.[-\w]+)* # remaining subdomains or domain |
|---|
| 234 |
(?::\d+)? # port |
|---|
| 235 |
(?:/(?:(?:[\~\w\+%-]|(?:[,.;:][^\s$]))+)?)* # path |
|---|
| 236 |
(?:\?[\w\+%&=.;-]+)? # query string |
|---|
| 237 |
(?:\#[\w\-/\?!=]*)? # trailing anchor |
|---|
| 238 |
) |
|---|
| 239 |
([[:punct:]]|\s|<|$) # trailing text |
|---|
| 240 |
~x'); |
|---|
| 241 |
} |
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
function _auto_link_urls($text, $href_options = array(), $truncate = false, $truncate_len = 40, $pad = '...') |
|---|
| 247 |
{ |
|---|
| 248 |
$href_options = _tag_options($href_options); |
|---|
| 249 |
|
|---|
| 250 |
$callback_function = ' |
|---|
| 251 |
if (preg_match("/<a\s/i", $matches[1])) |
|---|
| 252 |
{ |
|---|
| 253 |
return $matches[0]; |
|---|
| 254 |
} |
|---|
| 255 |
'; |
|---|
| 256 |
|
|---|
| 257 |
if ($truncate) |
|---|
| 258 |
{ |
|---|
| 259 |
$callback_function .= ' |
|---|
| 260 |
else if (strlen($matches[2].$matches[3]) > '.$truncate_len.') |
|---|
| 261 |
{ |
|---|
| 262 |
return $matches[1].\'<a href="\'.($matches[2] == "www." ? "http://www." : $matches[2]).$matches[3].\'"'.$href_options.'>\'.substr($matches[2].$matches[3], 0, '.$truncate_len.').\''.$pad.'</a>\'.$matches[4]; |
|---|
| 263 |
} |
|---|
| 264 |
'; |
|---|
| 265 |
} |
|---|
| 266 |
|
|---|
| 267 |
$callback_function .= ' |
|---|
| 268 |
else |
|---|
| 269 |
{ |
|---|
| 270 |
return $matches[1].\'<a href="\'.($matches[2] == "www." ? "http://www." : $matches[2]).$matches[3].\'"'.$href_options.'>\'.$matches[2].$matches[3].\'</a>\'.$matches[4]; |
|---|
| 271 |
} |
|---|
| 272 |
'; |
|---|
| 273 |
|
|---|
| 274 |
return preg_replace_callback( |
|---|
| 275 |
SF_AUTO_LINK_RE, |
|---|
| 276 |
create_function('$matches', $callback_function), |
|---|
| 277 |
$text |
|---|
| 278 |
); |
|---|
| 279 |
} |
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
function _auto_link_email_addresses($text) |
|---|
| 285 |
{ |
|---|
| 286 |
return preg_replace('/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/', '<a href="mailto:\\1">\\1</a>', $text); |
|---|
| 287 |
} |
|---|
| 288 |
|
|---|