123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?php
- function truncate_text($text, $length = 30, $truncate_string = '...', $truncate_lastspace = false)
- {
- if ($text == '')
- {
- return '';
- }
- $mbstring = extension_loaded('mbstring');
- if($mbstring)
- {
- @mb_internal_encoding(mb_detect_encoding($text));
- }
- $strlen = ($mbstring) ? 'mb_strlen' : 'strlen';
- $substr = ($mbstring) ? 'mb_substr' : 'substr';
- if ($strlen($text) > $length)
- {
- $truncate_text = $substr($text, 0, $length - $strlen($truncate_string));
- if ($truncate_lastspace)
- {
- $truncate_text = preg_replace('/\s+?(\S+)?$/', '', $truncate_text);
- }
- return $truncate_text.$truncate_string;
- }
- else
- {
- return $text;
- }
- }
- function highlight_text($text, $phrase, $highlighter = '<strong class="highlight">\\1</strong>')
- {
- if (empty($text))
- {
- return '';
- }
- if (empty($phrase))
- {
- return $text;
- }
- if (is_array($phrase) or ($phrase instanceof sfOutputEscaperArrayDecorator))
- {
- foreach ($phrase as $word)
- {
- $pattern[] = '/('.preg_quote($word, '/').')/i';
- $replacement[] = $highlighter;
- }
- }
- else
- {
- $pattern = '/('.preg_quote($phrase, '/').')/i';
- $replacement = $highlighter;
- }
- return preg_replace($pattern, $replacement, $text);
- }
- function excerpt_text($text, $phrase, $radius = 100, $excerpt_string = '...', $excerpt_space = false)
- {
- if ($text == '' || $phrase == '')
- {
- return '';
- }
- $mbstring = extension_loaded('mbstring');
- if($mbstring)
- {
- @mb_internal_encoding(mb_detect_encoding($text));
- }
- $strlen = ($mbstring) ? 'mb_strlen' : 'strlen';
- $strpos = ($mbstring) ? 'mb_strpos' : 'strpos';
- $strtolower = ($mbstring) ? 'mb_strtolower' : 'strtolower';
- $substr = ($mbstring) ? 'mb_substr' : 'substr';
- $found_pos = $strpos($strtolower($text), $strtolower($phrase));
- if ($found_pos !== false)
- {
- $start_pos = max($found_pos - $radius, 0);
- $end_pos = min($found_pos + $strlen($phrase) + $radius, $strlen($text));
- $excerpt = $substr($text, $start_pos, $end_pos - $start_pos);
- $prefix = ($start_pos > 0) ? $excerpt_string : '';
- $postfix = $end_pos < $strlen($text) ? $excerpt_string : '';
- if ($excerpt_space)
- {
-
- if($prefix)
- {
- $excerpt = preg_replace('/^(\S+)?\s+?/', ' ', $excerpt);
- }
- if($postfix)
- {
- $excerpt = preg_replace('/\s+?(\S+)?$/', ' ', $excerpt);
- }
- }
- return $prefix.$excerpt.$postfix;
- }
- }
- function wrap_text($text, $line_width = 80)
- {
- return preg_replace('/(.{1,'.$line_width.'})(\s+|$)/s', "\\1\n", preg_replace("/\n/", "\n\n", $text));
- }
- function simple_format_text($text, $options = array())
- {
- $css = (isset($options['class'])) ? ' class="'.$options['class'].'"' : '';
- $text = sfToolkit::pregtr($text, array("/(\r\n|\r)/" => "\n",
- "/\n{3,}/" => "\n\n",
- "/\n\n/" => "</p>\\0<p$css>",
- "/([^\n])\n([^\n])/" => "\\1\n<br />\\2"));
- return '<p'.$css.'>'.$text.'</p>';
- }
- function auto_link_text($text, $link = 'all', $href_options = array(), $truncate = false, $truncate_len = 35, $pad = '...')
- {
- if ($link == 'all')
- {
- return _auto_link_urls(_auto_link_email_addresses($text), $href_options, $truncate, $truncate_len, $pad);
- }
- else if ($link == 'email_addresses')
- {
- return _auto_link_email_addresses($text);
- }
- else if ($link == 'urls')
- {
- return _auto_link_urls($text, $href_options, $truncate, $truncate_len, $pad);
- }
- }
- function strip_links_text($text)
- {
- return preg_replace('/<a.*>(.*)<\/a>/m', '\\1', $text);
- }
- if (!defined('SF_AUTO_LINK_RE'))
- {
- define('SF_AUTO_LINK_RE', '~
- ( # leading text
- <\w+.*?>| # leading HTML tag, or
- [^=!:\'"/]| # leading punctuation, or
- ^ # beginning of line
- )
- (
- (?:https?://)| # protocol spec, or
- (?:www\.) # www.*
- )
- (
- [-\w]+ # subdomain or domain
- (?:\.[-\w]+)* # remaining subdomains or domain
- (?::\d+)? # port
- (?:/(?:(?:[\~\w\+%-]|(?:[,.;:][^\s$]))+)?)* # path
- (?:\?[\w\+%&=.;-]+)? # query string
- (?:\#[\w\-]*)? # trailing anchor
- )
- ([[:punct:]]|\s|<|$) # trailing text
- ~x');
- }
- function _auto_link_urls($text, $href_options = array(), $truncate = false, $truncate_len = 40, $pad = '...')
- {
- $href_options = _tag_options($href_options);
- $callback_function = '
- if (preg_match("/<a\s/i", $matches[1]))
- {
- return $matches[0];
- }
- ';
- if ($truncate)
- {
- $callback_function .= '
- else if (strlen($matches[2].$matches[3]) > '.$truncate_len.')
- {
- 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];
- }
- ';
- }
- $callback_function .= '
- else
- {
- return $matches[1].\'<a href="\'.($matches[2] == "www." ? "http://www." : $matches[2]).$matches[3].\'"'.$href_options.'>\'.$matches[2].$matches[3].\'</a>\'.$matches[4];
- }
- ';
- return preg_replace_callback(
- SF_AUTO_LINK_RE,
- create_function('$matches', $callback_function),
- $text
- );
- }
- function _auto_link_email_addresses($text)
- {
- return preg_replace('/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/', '<a href="mailto:\\1">\\1</a>', $text);
- }
|