123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php namespace System;
- class Text {
-
- public static function words($value, $limit, $end = '…')
- {
- if (trim($value) == '')
- {
- return $value;
- }
-
-
-
- preg_match('/^\s*+(?:\S++\s*+){1,'.$limit.'}/', $value, $matches);
-
-
-
-
- if (strlen($value) == strlen($matches[0]))
- {
- $end = '';
- }
- return rtrim($matches[0]).$end;
- }
-
- public static function characters($value, $limit, $end = '…')
- {
- if (strlen($value) < $limit)
- {
- return $value;
- }
-
-
-
- $value = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $value));
- if (strlen($value) <= $limit)
- {
- return $value;
- }
- $out = '';
-
-
-
-
- foreach (explode(' ', trim($value)) as $val)
- {
- $out .= $val.' ';
- if (strlen($out) >= $limit)
- {
- $out = trim($out);
- return (strlen($out) == strlen($value)) ? $out : $out.$end;
- }
- }
- }
-
- public static function censor($value, $censored, $replacement = '####')
- {
- $value = ' '.$value.' ';
-
-
-
- $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
-
-
-
- foreach ($censored as $word)
- {
- if ($replacement != '')
- {
- $value = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($word, '/')).")({$delim})/i", "\\1{$replacement}\\3", $value);
- }
- else
- {
- $value = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($word, '/')).")({$delim})/ie", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'", $value);
- }
- }
- return trim($value);
- }
- }
|