Changeset 454 for trunk/lib/Utilities.inc.php
- Timestamp:
- Jan 4, 2014 3:25:57 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Utilities.inc.php
r419 r454 4 4 * For details visit the project site: <http://trac.strangecode.com/codebase/> 5 5 * Copyright 2001-2012 Strangecode, LLC 6 * 6 * 7 7 * This file is part of The Strangecode Codebase. 8 8 * … … 11 11 * Free Software Foundation, either version 3 of the License, or (at your option) 12 12 * any later version. 13 * 13 * 14 14 * The Strangecode Codebase is distributed in the hope that it will be useful, but 15 15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 16 16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 17 * details. 18 * 18 * 19 19 * You should have received a copy of the GNU General Public License along with 20 20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. … … 37 37 if (defined('_CLI')) { 38 38 echo "\n"; 39 } else { 39 } else { 40 40 echo $display ? "\n<br /><pre>\n" : "\n\n\n<!--\n"; 41 41 } … … 47 47 if (defined('_CLI')) { 48 48 echo "\n"; 49 } else { 49 } else { 50 50 echo $display ? "\n</pre><br />\n" : "\n-->\n\n\n"; 51 51 } … … 180 180 { 181 181 $words = preg_split('/[^\w]/', $search, -1, PREG_SPLIT_NO_EMPTY); 182 182 183 183 $search = array(); 184 184 $replace = array(); 185 185 186 186 foreach ($words as $w) { 187 187 if ('' != trim($w)) { … … 266 266 267 267 /** 268 * Turns "a really long string" into "a rea...string" 268 * Truncates "a really long string" into a string of specified length 269 * at the beginning: "âŠlong string" 270 * at the middle: "a reaâŠstring" 271 * or at the end: "a reallyâŠ". 272 * 273 * The regular expressions below first match and replace the string to the specified length and position, 274 * and secondly they remove any whitespace from around the delimiter (to avoid "this ⊠" from happening). 269 275 * 270 276 * @access public … … 272 278 * @param int $len Maximum string length. 273 279 * @param string $where Where to cut the string. One of: 'start', 'middle', or 'end'. 274 * @return string Truncated output string 280 * @return string Truncated output string. 275 281 * @author Quinn Comendant <quinn@strangecode.com> 276 282 * @since 29 Mar 2006 13:48:49 277 283 */ 278 function truncate($str, $len, $where='end', $delim='âŠ') 279 { 280 if ($len <= 3 || mb_strlen($str) <= 3) { 281 return ''; 282 } 283 $part1 = floor(($len - 3) / 2); 284 $part2 = ceil(($len - 3) / 2); 284 function truncate($str, $len=50, $where='end', $delim='âŠ') 285 { 286 $dlen = mb_strlen($delim); 287 if ($len <= $dlen || mb_strlen($str) <= $dlen) { 288 return substr($str, 0, $len); 289 } 290 $part1 = floor(($len - $dlen) / 2); 291 $part2 = ceil(($len - $dlen) / 2); 285 292 switch ($where) { 286 293 case 'start' : 287 return preg_replace(array(sprintf('/^.{4,}(.{%s})$/sU', $part1 + $part2), '/\s*\.{3,}\s*/sU'), array($delim . '$1', $delim), $str); 288 break; 294 return preg_replace(array(sprintf('/^.{%s,}(.{%s})$/sU', $dlen + 1, $part1 + $part2), sprintf('/\s*%s{%s,}\s*/sU', preg_quote($delim), $dlen)), array($delim . '$1', $delim), $str); 295 296 case 'middle' : 297 return preg_replace(array(sprintf('/^(.{%s}).{%s,}(.{%s})$/sU', $part1, $dlen + 1, $part2), sprintf('/\s*%s{%s,}\s*/sU', preg_quote($delim), $dlen)), array('$1' . $delim . '$2', $delim), $str); 298 299 case 'end' : 289 300 default : 290 case 'middle' : 291 return preg_replace(array(sprintf('/^(.{%s}).{4,}(.{%s})$/sU', $part1, $part2), '/\s*\.{3,}\s*/sU'), array('$1' . $delim . '$2', $delim), $str); 292 break; 293 case 'end' : 294 return preg_replace(array(sprintf('/^(.{%s}).{4,}$/sU', $part1 + $part2), '/\s*\.{3,}\s*/sU'), array('$1' . $delim, $delim), $str); 295 break; 301 return preg_replace(array(sprintf('/^(.{%s}).{%s,}$/sU', $part1 + $part2, $dlen + 1), sprintf('/\s*%s{%s,}\s*/sU', preg_quote($delim), $dlen)), array('$1' . $delim, $delim), $str); 296 302 } 297 303 } … … 307 313 * @since 06 Dec 2008 17:04:01 308 314 */ 309 if (!function_exists('mb_ucfirst')) { 315 if (!function_exists('mb_ucfirst')) { 310 316 function mb_ucfirst($string) 311 317 { … … 326 332 * @since 20 Jan 2013 12:33:26 327 333 */ 328 if (!function_exists('mb_strtr')) { 334 if (!function_exists('mb_strtr')) { 329 335 function mb_strtr($string, $from, $to) 330 336 { … … 367 373 /* 368 374 * Returns a human readable amount of time for the given amount of seconds. 369 * 375 * 370 376 * 45 seconds 371 377 * 12 minutes … … 374 380 * 1 week 375 381 * 4 months 376 * 382 * 377 383 * Months are calculated using the real number of days in a year: 365.2422 / 12. 378 384 * … … 400 406 'century' => array(3155692608, _("century"), _("centuries")), 401 407 ); 402 408 403 409 // Max unit to calculate. 404 410 $max_unit = isset($units[$max_unit]) ? $max_unit : 'year'; … … 416 422 } 417 423 $final_time = sprintf($format, $final_time); 418 return sprintf('%s %s', $final_time, (1 == $final_time ? $units[$final_unit][1] : $units[$final_unit][2])); 424 return sprintf('%s %s', $final_time, (1 == $final_time ? $units[$final_unit][1] : $units[$final_unit][2])); 419 425 } 420 426 … … 430 436 { 431 437 $app =& App::getInstance(); 432 438 433 439 foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) { 434 440 $fullpath = $path . DIRECTORY_SEPARATOR . $file; … … 680 686 * escaped. This is useful for putting values coming in from posted 681 687 * checkboxes into a SET column of a database. 682 * 688 * 683 689 * 684 690 * @param array $in Array to convert. … … 688 694 { 689 695 $db =& DB::getInstance(); 690 696 691 697 if (is_array($in) && !empty($in)) { 692 698 return join($separator, array_map(array($db, 'escapeString'), $in)); … … 839 845 { 840 846 $app =& App::getInstance(); 841 847 842 848 if ('' == trim($val)) { 843 849 $app->logMsg(sprintf('Cannot add signature to an empty string.', null), LOG_INFO, __FILE__, __LINE__); … … 848 854 $salt = $app->getParam('signing_key'); 849 855 } 850 856 851 857 // TODO: consider using more bits-per-character, such as done with: 852 858 // http://www.php.net/manual/en/function.sha1.php#86239 … … 916 922 { 917 923 $app =& App::getInstance(); 918 924 919 925 $add_members = '/usr/lib/mailman/bin/add_members'; 920 926 /// FIXME: checking of executable is disabled. … … 948 954 { 949 955 $app =& App::getInstance(); 950 956 951 957 $remove_members = '/usr/lib/mailman/bin/remove_members'; 952 958 /// FIXME: checking of executable is disabled.
Note: See TracChangeset
for help on using the changeset viewer.