Changeset 845 in ExiteCMS for trunk/includes/core_functions.php


Ignore:
Timestamp:
09/28/07 00:02:59 (5 years ago)
Author:
hverton
Message:

new shortenlink() function to trim URL's in the middle
fixed positioning of the IP icon in the renderpost templates
reworked URL detection in messages (still has an issue with a trailing dot!)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/core_functions.php

    r843 r845  
    268268 
    269269// Trim a line of text to a preferred length 
    270 function trimlink($text, $length) { 
     270function trimlink($text, $length, $filler="...") { 
    271271    $dec = array("\"", "'", "\\", '\"', "\'", "<", ">"); 
    272272    $enc = array("&quot;", "&#39;", "&#92;", "&quot;", "&#39;", "&lt;", "&gt;"); 
    273273    $text = str_replace($enc, $dec, $text); 
    274     if (strlen($text) > $length) $text = substr($text, 0, ($length-3))."..."; 
     274    if (strlen($text) > $length) $text = substr($text, 0, ($length-3)).$filler; 
    275275    $text = str_replace($dec, $enc, $text); 
    276276    return $text; 
     277} 
     278 
     279// Trim a URI to a preferred length by cutting out the middle (preserve the hostname if possible) 
     280function shortenlink($text, $length, $filler="...") { 
     281 
     282    $dec = array("\"", "'", "\\", '\"', "\'", "<", ">"); 
     283    $enc = array("&quot;", "&#39;", "&#92;", "&quot;", "&#39;", "&lt;", "&gt;"); 
     284    $returner = str_replace($enc, $dec, $text); 
     285    if (strlen($returner) > $length) { 
     286        $url = preg_match("=[^/]/[^/]=",$returner,$treffer,PREG_OFFSET_CAPTURE); 
     287        $cutpos = $treffer[0][1]+2; 
     288        $part[0] = substr($returner,0,$cutpos); 
     289        $part[1] = substr($returner,$cutpos); 
     290        $strlen1 = $cutpos; 
     291        if ($strlen1 > $length) { 
     292            $returner = substr($returner,0,$length-3).$filler; 
     293        } else { 
     294            $strlen2 = strlen($part[1]); 
     295            $cutpos = $strlen2-($length-3-$strlen1); 
     296            $returner = $part[0].$filler.substr($part[1],$cutpos); 
     297        } 
     298    } 
     299    $returner = str_replace($dec, $enc, $returner); 
     300    return $returner; 
    277301} 
    278302 
     
    490514    $text = preg_replace('#\[url=([\r\n]*)(http://|ftp://|https://|ftps://)([^\'\";]*?)\](.*?)([\r\n]*)\[/url\]#si', '<a href=\'\2\3\' target=\'_blank\'>\4</a>', $text); 
    491515    $text = preg_replace('#\[url=([\r\n]*)([^\s\'\";\+]*?)\](.*?)([\r\n]*)\[/url\]#si', '<a href=\'http://\2\' target=\'_blank\'>\3</a>', $text); 
    492      
     516 
    493517    $text = preg_replace('#\[mail\]([\r\n]*)([^\s\'\";:\+]*?)([\r\n]*)\[/mail\]#si', '<a href=\'mailto:\2\'>\2</a>', $text); 
    494518    $text = preg_replace('#\[mail=([\r\n]*)([^\s\'\";:\+]*?)\](.*?)([\r\n]*)\[/mail\]#si', '<a href=\'mailto:\2\'>\2</a>', $text); 
Note: See TracChangeset for help on using the changeset viewer.