ad_string_truncate (public)
ad_string_truncate [ -len len ] [ -ellipsis ellipsis ] [ -more more ] \ [ -equal ] string
Defined in packages/acs-tcl/tcl/text-html-procs.tcl
Truncates a string to len characters adding the string provided in the ellipsis parameter if the string was truncated. The length of the resulting string, including the ellipsis, is guaranteed to be shorter or equal than the len specified. Should always be called as ad_string_truncate [-flags ...] -- string since otherwise strings which start with a - will treated as switches, and will cause an error.
- Switches:
- -len (optional, defaults to
"200"
)- The length to truncate to. If zero, no truncation will occur.
- -ellipsis (optional, defaults to
"..."
)- This will get put at the end of the truncated string, if the string was truncated. However, this counts towards the total string length, so that the returned string including ellipsis is guaranteed to be shorter or equal than the 'len' provided.
- -more (optional)
- This will get put at the end of the truncated string, if the string was truncated.
- -equal (optional, boolean)
- Parameters:
- string (required)
- The string to truncate.
- Returns:
- The truncated string
- Author:
- Lars Pind <lars@pinds.com>
- Created:
- September 8, 2002
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- ad_string_truncate
Source code: if { $len > 0 & [string length $string] > $len } { set end_index [expr {$len-[string length $ellipsis]-1}] # Back up to the nearest whitespace if {[regexp -indices {\s\S*$} [string range $string 0 $end_index+1] match]} { set last_space [lindex $match 0] } else { set last_space -1 } # If that leaves us with an empty string, then ignore # whitespace and just truncate mid-word set end_index [expr {$last_space > 0 ? $last_space : $end_index}] # Chop off extra whitespace at the end set string [string trimright [string range $string 0 $end_index]]${ellipsis}${more} } return $stringXQL Not present: Generic, PostgreSQL, Oracle