Changeset 467 for branches/eli_branch/lib/Utilities.inc.php
- Timestamp:
- Feb 19, 2014 2:28:00 AM (10 years ago)
- Location:
- branches/eli_branch
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eli_branch
-
branches/eli_branch/lib/Utilities.inc.php
r439 r467 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/>. … … 33 33 * @param bool $var_dump Use var_dump instead of print_r. 34 34 */ 35 function dump($var, $display=false, $var_dump=false) 36 { 35 function dump($var, $display=false, $var_dump=false, $file='', $line='') 36 { 37 if (defined('_CLI')) { 38 echo "\n$file $line\n"; 39 } else { 40 echo $display ? "\n<br /><strong>$file $line</strong>\n<br /><pre>\n" : "\n\n\n<!--\n$file $line\n"; 41 } 42 if ($var_dump) { 43 var_dump($var); 44 } else { 45 print_r($var); 46 } 37 47 if (defined('_CLI')) { 38 48 echo "\n"; 39 } else { 40 echo $display ? "\n<br /><pre>\n" : "\n\n\n<!--\n"; 41 } 42 if ($var_dump) { 43 var_dump($var); 44 } else { 45 print_r($var); 46 } 47 if (defined('_CLI')) { 48 echo "\n"; 49 } else { 49 } else { 50 50 echo $display ? "\n</pre><br />\n" : "\n-->\n\n\n"; 51 51 } 52 52 } 53 53 54 /** 55 * Return dump as variable. 56 * 57 * @param mixed $var Variable to dump. 58 * @param bool $serialize Remove line-endings. Useful for logging variables. 59 * @return string Dump of var. 60 */ 54 /* 55 * Log a PHP variable to javascript console. Relies on getDump(), below. 56 * 57 * @access public 58 * @param mixed $var The variable to dump. 59 * @param string $prefix A short note to print before the output to make identifying output easier. 60 * @param string $file The value of __FILE__. 61 * @param string $line The value of __LINE__. 62 * @return null 63 * @author Quinn Comendant <quinn@strangecode.com> 64 */ 65 function jsDump($var, $prefix='jsDump', $file='-', $line='-') 66 { 67 if (!empty($var)) { 68 ?> 69 <script type="text/javascript" charset="utf-8"> 70 /* <![CDATA[ */ 71 window.console && console.log('<?php printf('%s: %s (on line %s of %s)', $prefix, str_replace("'", "\\'", getDump($var, true)), $line, $file); ?>'); 72 /* ]]> */ 73 </script> 74 <?php 75 } 76 } 77 78 /* 79 * Return a string version of any variable, optionally serialized on one line. 80 * 81 * @access public 82 * @param mixed $var The variable to dump. 83 * @param bool $serialize If true, remove line-endings. Useful for logging variables. 84 * @return string The dumped variable. 85 * @author Quinn Comendant <quinn@strangecode.com> 86 */ 61 87 function getDump($var, $serialize=false) 62 88 { … … 65 91 $d = ob_get_contents(); 66 92 ob_end_clean(); 67 return $serialize ? preg_replace('/\s+/m', ' ', $d) : $d;93 return $serialize ? preg_replace('/\s+/m', ' ', $d) : $d; 68 94 } 69 95 … … 180 206 { 181 207 $words = preg_split('/[^\w]/', $search, -1, PREG_SPLIT_NO_EMPTY); 182 208 183 209 $search = array(); 184 210 $replace = array(); 185 211 186 212 foreach ($words as $w) { 187 213 if ('' != trim($w)) { … … 266 292 267 293 /** 268 * Turns "a really long string" into "a rea...string" 294 * Truncates "a really long string" into a string of specified length 295 * at the beginning: "âŠlong string" 296 * at the middle: "a reaâŠstring" 297 * or at the end: "a reallyâŠ". 298 * 299 * The regular expressions below first match and replace the string to the specified length and position, 300 * and secondly they remove any whitespace from around the delimiter (to avoid "this ⊠" from happening). 269 301 * 270 302 * @access public … … 272 304 * @param int $len Maximum string length. 273 305 * @param string $where Where to cut the string. One of: 'start', 'middle', or 'end'. 274 * @return string Truncated output string 306 * @return string Truncated output string. 275 307 * @author Quinn Comendant <quinn@strangecode.com> 276 308 * @since 29 Mar 2006 13:48:49 277 309 */ 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); 310 function truncate($str, $len=50, $where='end', $delim='âŠ') 311 { 312 $dlen = mb_strlen($delim); 313 if ($len <= $dlen || mb_strlen($str) <= $dlen) { 314 return substr($str, 0, $len); 315 } 316 $part1 = floor(($len - $dlen) / 2); 317 $part2 = ceil(($len - $dlen) / 2); 285 318 switch ($where) { 286 319 case 'start' : 287 return preg_replace(array(sprintf('/^.{4,}(.{%s})$/sU', $part1 + $part2), '/\s*\.{3,}\s*/sU'), array($delim . '$1', $delim), $str); 288 break; 320 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); 321 322 case 'middle' : 323 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); 324 325 case 'end' : 289 326 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; 327 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 328 } 297 329 } … … 307 339 * @since 06 Dec 2008 17:04:01 308 340 */ 309 if (!function_exists('mb_ucfirst')) { 341 if (!function_exists('mb_ucfirst')) { 310 342 function mb_ucfirst($string) 311 343 { … … 326 358 * @since 20 Jan 2013 12:33:26 327 359 */ 328 if (!function_exists('mb_strtr')) { 360 if (!function_exists('mb_strtr')) { 329 361 function mb_strtr($string, $from, $to) 330 362 { … … 367 399 /* 368 400 * Returns a human readable amount of time for the given amount of seconds. 369 * 401 * 370 402 * 45 seconds 371 403 * 12 minutes … … 374 406 * 1 week 375 407 * 4 months 376 * 408 * 377 409 * Months are calculated using the real number of days in a year: 365.2422 / 12. 378 410 * … … 400 432 'century' => array(3155692608, _("century"), _("centuries")), 401 433 ); 402 434 403 435 // Max unit to calculate. 404 436 $max_unit = isset($units[$max_unit]) ? $max_unit : 'year'; … … 416 448 } 417 449 $final_time = sprintf($format, $final_time); 418 return sprintf('%s %s', $final_time, (1 == $final_time ? $units[$final_unit][1] : $units[$final_unit][2])); 450 return sprintf('%s %s', $final_time, (1 == $final_time ? $units[$final_unit][1] : $units[$final_unit][2])); 419 451 } 420 452 … … 430 462 { 431 463 $app =& App::getInstance(); 432 464 433 465 foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) { 434 466 $fullpath = $path . DIRECTORY_SEPARATOR . $file; … … 680 712 * escaped. This is useful for putting values coming in from posted 681 713 * checkboxes into a SET column of a database. 682 * 714 * 683 715 * 684 716 * @param array $in Array to convert. … … 688 720 { 689 721 $db =& DB::getInstance(); 690 722 691 723 if (is_array($in) && !empty($in)) { 692 724 return join($separator, array_map(array($db, 'escapeString'), $in)); … … 839 871 { 840 872 $app =& App::getInstance(); 841 873 842 874 if ('' == trim($val)) { 843 875 $app->logMsg(sprintf('Cannot add signature to an empty string.', null), LOG_INFO, __FILE__, __LINE__); … … 848 880 $salt = $app->getParam('signing_key'); 849 881 } 850 882 851 883 // TODO: consider using more bits-per-character, such as done with: 852 884 // http://www.php.net/manual/en/function.sha1.php#86239 … … 916 948 { 917 949 $app =& App::getInstance(); 918 950 919 951 $add_members = '/usr/lib/mailman/bin/add_members'; 920 952 /// FIXME: checking of executable is disabled. … … 948 980 { 949 981 $app =& App::getInstance(); 950 982 951 983 $remove_members = '/usr/lib/mailman/bin/remove_members'; 952 984 /// FIXME: checking of executable is disabled.
Note: See TracChangeset
for help on using the changeset viewer.