Ignore:
Timestamp:
Jan 4, 2014 3:25:57 AM (10 years ago)
Author:
anonymous
Message:

Improved the truncate() function.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Utilities.inc.php

    r419 r454  
    44 * For details visit the project site: <http://trac.strangecode.com/codebase/>
    55 * Copyright 2001-2012 Strangecode, LLC
    6  * 
     6 *
    77 * This file is part of The Strangecode Codebase.
    88 *
     
    1111 * Free Software Foundation, either version 3 of the License, or (at your option)
    1212 * any later version.
    13  * 
     13 *
    1414 * The Strangecode Codebase is distributed in the hope that it will be useful, but
    1515 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1616 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    1717 * details.
    18  * 
     18 *
    1919 * You should have received a copy of the GNU General Public License along with
    2020 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
     
    3737    if (defined('_CLI')) {
    3838        echo "\n";
    39     } else {       
     39    } else {
    4040        echo $display ? "\n<br /><pre>\n" : "\n\n\n<!--\n";
    4141    }
     
    4747    if (defined('_CLI')) {
    4848        echo "\n";
    49     } else {       
     49    } else {
    5050        echo $display ?  "\n</pre><br />\n" : "\n-->\n\n\n";
    5151    }
     
    180180{
    181181    $words = preg_split('/[^\w]/', $search, -1, PREG_SPLIT_NO_EMPTY);
    182    
     182
    183183    $search = array();
    184184    $replace = array();
    185    
     185
    186186    foreach ($words as $w) {
    187187        if ('' != trim($w)) {
     
    266266
    267267/**
    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).
    269275 *
    270276 * @access  public
     
    272278 * @param   int     $len    Maximum string length.
    273279 * @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.
    275281 * @author  Quinn Comendant <quinn@strangecode.com>
    276282 * @since   29 Mar 2006 13:48:49
    277283 */
    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);
     284function 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);
    285292    switch ($where) {
    286293    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' :
    289300    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);
    296302    }
    297303}
     
    307313* @since    06 Dec 2008 17:04:01
    308314*/
    309 if (!function_exists('mb_ucfirst')) {   
     315if (!function_exists('mb_ucfirst')) {
    310316    function mb_ucfirst($string)
    311317    {
     
    326332* @since    20 Jan 2013 12:33:26
    327333*/
    328 if (!function_exists('mb_strtr')) {   
     334if (!function_exists('mb_strtr')) {
    329335    function mb_strtr($string, $from, $to)
    330336    {
     
    367373/*
    368374* Returns a human readable amount of time for the given amount of seconds.
    369 * 
     375*
    370376* 45 seconds
    371377* 12 minutes
     
    374380* 1 week
    375381* 4 months
    376 * 
     382*
    377383* Months are calculated using the real number of days in a year: 365.2422 / 12.
    378384*
     
    400406        'century' => array(3155692608, _("century"), _("centuries")),
    401407    );
    402    
     408
    403409    // Max unit to calculate.
    404410    $max_unit = isset($units[$max_unit]) ? $max_unit : 'year';
     
    416422    }
    417423    $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]));
    419425}
    420426
     
    430436{
    431437    $app =& App::getInstance();
    432    
     438
    433439    foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) {
    434440        $fullpath = $path . DIRECTORY_SEPARATOR . $file;
     
    680686 * escaped.  This is useful for putting values coming in from posted
    681687 * checkboxes into a SET column of a database.
    682  * 
     688 *
    683689 *
    684690 * @param  array $in      Array to convert.
     
    688694{
    689695    $db =& DB::getInstance();
    690    
     696
    691697    if (is_array($in) && !empty($in)) {
    692698        return join($separator, array_map(array($db, 'escapeString'), $in));
     
    839845{
    840846    $app =& App::getInstance();
    841    
     847
    842848    if ('' == trim($val)) {
    843849        $app->logMsg(sprintf('Cannot add signature to an empty string.', null), LOG_INFO, __FILE__, __LINE__);
     
    848854        $salt = $app->getParam('signing_key');
    849855    }
    850    
     856
    851857    // TODO: consider using more bits-per-character, such as done with:
    852858    // http://www.php.net/manual/en/function.sha1.php#86239
     
    916922{
    917923    $app =& App::getInstance();
    918    
     924
    919925    $add_members = '/usr/lib/mailman/bin/add_members';
    920926    /// FIXME: checking of executable is disabled.
     
    948954{
    949955    $app =& App::getInstance();
    950    
     956
    951957    $remove_members = '/usr/lib/mailman/bin/remove_members';
    952958    /// FIXME: checking of executable is disabled.
Note: See TracChangeset for help on using the changeset viewer.