| 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 |
if (strlen($text) > $length) |
|---|
| 34 |
{ |
|---|
| 35 |
$truncate_text = substr($text, 0, $length - strlen($truncate_string)); |
|---|
| 36 |
if ($truncate_lastspace) |
|---|
| 37 |
{ |
|---|
| 38 |
$truncate_text = preg_replace('/\s+?(\S+)?$/', '', $truncate_text); |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
return $truncate_text.$truncate_string; |
|---|
| 42 |
} |
|---|
| 43 |
else |
|---|
| 44 |
{ |
|---|
| 45 |
return $text; |
|---|
| 46 |
} |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
function highlight_text($text, $phrase, $highlighter = '<strong class="highlight">\\1</strong>') |
|---|
| 56 |
{ |
|---|
| 57 |
if ($text == '') |
|---|
| 58 |
{ |
|---|
| 59 |
return ''; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
if ($phrase == '') |
|---|
| 63 |
{ |
|---|
| 64 |
return $text; |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
return preg_replace('/('.preg_quote($phrase, '/').')/i', $highlighter, $text); |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
function excerpt_text($text, $phrase, $radius = 100, $excerpt_string = '...', $excerpt_space = false) |
|---|
| 79 |
{ |
|---|
| 80 |
if ($text == '' || $phrase == '') |
|---|
| 81 |
{ |
|---|
| 82 |
return ''; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
$found_pos = strpos(strtolower($text), strtolower($phrase)); |
|---|
| 86 |
if ($found_pos !== false) |
|---|
| 87 |
{ |
|---|
| 88 |
$start_pos = max($found_pos - $radius, 0); |
|---|
| 89 |
$end_pos = min($found_pos + strlen($phrase) + $radius, strlen($text)); |
|---|
| 90 |
$excerpt = substr($text, $start_pos, $end_pos - $start_pos); |
|---|
| 91 |
|
|---|
| 92 |
$prefix = ($start_pos > 0) ? $excerpt_string : ''; |
|---|
| 93 |
$postfix = $end_pos < strlen($text) ? $excerpt_string : ''; |
|---|
| 94 |
|
|---|
| 95 |
if ($excerpt_space) |
|---|
| 96 |
{ |
|---|
| 97 |
|
|---|
| 98 |
if($prefix) |
|---|
| 99 |
{ |
|---|
| 100 |
$excerpt = preg_replace('/^(\S+)?\s+?/', ' ', $excerpt); |
|---|
| 101 |
} |
|---|
| 102 |
if($postfix) |
|---|
| 103 |
{ |
|---|
| 104 |
$excerpt = preg_replace('/\s+?(\S+)?$/', ' ', $excerpt); |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
return $prefix.$excerpt.$postfix; |
|---|
| 110 |
} |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
function wrap_text($text, $line_width = 80) |
|---|
| 117 |
{ |
|---|
| 118 |
return preg_replace('/(.{1,'.$line_width.'})(\s+|$)/s', "\\1\n", preg_replace("/\n/", "\n\n", $text)); |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
function simple_format_text($text, $options = array()) |
|---|
| 128 |
{ |
|---|
| 129 |
$css = (isset($options['class'])) ? ' class="'.$options['class'].'"' : ''; |
|---|
| 130 |
|
|---|
| 131 |
$text = sfToolkit::pregtr($text, array("/(\r\n|\r)/" => "\n", |
|---|
| 132 |
"/\n{3,}/" => "\n\n", |
|---|
| 133 |
"/\n\n/" => "</p>\\0<p$css>", |
|---|
| 134 |
"/([^\n])\n([^\n])/" => "\\1\n<br />\\2")); |
|---|
| 135 |
|
|---|
| 136 |
return '<p'.$css.'>'.$text.'</p>'; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
function auto_link_text($text, $link = 'all', $href_options = array()) |
|---|
| 149 |
{ |
|---|
| 150 |
if ($link == 'all') |
|---|
| 151 |
{ |
|---|
| 152 |
return _auto_link_urls(_auto_link_email_addresses($text), $href_options); |
|---|
| 153 |
} |
|---|
| 154 |
else if ($link == 'email_addresses') |
|---|
| 155 |
{ |
|---|
| 156 |
return _auto_link_email_addresses($text); |
|---|
| 157 |
} |
|---|
| 158 |
else if ($link == 'urls') |
|---|
| 159 |
{ |
|---|
| 160 |
return _auto_link_urls($text, $href_options); |
|---|
| 161 |
} |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
function strip_links_text($text) |
|---|
| 168 |
{ |
|---|
| 169 |
return preg_replace('/<a.*>(.*)<\/a>/m', '\\1', $text); |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
if (!defined('SF_AUTO_LINK_RE')) |
|---|
| 173 |
{ |
|---|
| 174 |
define('SF_AUTO_LINK_RE', '~ |
|---|
| 175 |
( # leading text |
|---|
| 176 |
<\w+.*?>| # leading HTML tag, or |
|---|
| 177 |
[^=!:\'"/]| # leading punctuation, or |
|---|
| 178 |
^ # beginning of line |
|---|
| 179 |
) |
|---|
| 180 |
( |
|---|
| 181 |
(?:https?://)| # protocol spec, or |
|---|
| 182 |
(?:www\.) # www.* |
|---|
| 183 |
) |
|---|
| 184 |
( |
|---|
| 185 |
[-\w]+ # subdomain or domain |
|---|
| 186 |
(?:\.[-\w]+)* # remaining subdomains or domain |
|---|
| 187 |
(?::\d+)? # port |
|---|
| 188 |
(?:/(?:(?:[\~\w\+%-]|(?:[,.;:][^\s$]))+)?)* # path |
|---|
| 189 |
(?:\?[\w\+%&=.;-]+)? # query string |
|---|
| 190 |
(?:\#[\w\-]*)? # trailing anchor |
|---|
| 191 |
) |
|---|
| 192 |
([[:punct:]]|\s|<|$) # trailing text |
|---|
| 193 |
~x'); |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
function _auto_link_urls($text, $href_options = array()) |
|---|
| 200 |
{ |
|---|
| 201 |
$href_options = _tag_options($href_options); |
|---|
| 202 |
return preg_replace_callback( |
|---|
| 203 |
SF_AUTO_LINK_RE, |
|---|
| 204 |
create_function('$matches', ' |
|---|
| 205 |
if (preg_match("/<a\s/i", $matches[1])) |
|---|
| 206 |
{ |
|---|
| 207 |
return $matches[0]; |
|---|
| 208 |
} |
|---|
| 209 |
else |
|---|
| 210 |
{ |
|---|
| 211 |
return $matches[1].\'<a href="\'.($matches[2] == "www." ? "http://www." : $matches[2]).$matches[3].\'"'.$href_options.'>\'.$matches[2].$matches[3].\'</a>\'.$matches[4]; |
|---|
| 212 |
} |
|---|
| 213 |
') |
|---|
| 214 |
, $text); |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
function _auto_link_email_addresses($text) |
|---|
| 221 |
{ |
|---|
| 222 |
return preg_replace('/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/', '<a href="mailto:\\1">\\1</a>', $text); |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|