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):
%3 test_ad_string_truncate ad_string_truncate (test acs-tcl) ad_string_truncate ad_string_truncate test_ad_string_truncate->ad_string_truncate ad_html_text_convert ad_html_text_convert (public) ad_html_text_convert->ad_string_truncate bug_tracker::bug::get_multirow bug_tracker::bug::get_multirow (public) bug_tracker::bug::get_multirow->ad_string_truncate notification::new notification::new (public) notification::new->ad_string_truncate packages/acs-admin/www/auth/authority.tcl packages/acs-admin/ www/auth/authority.tcl packages/acs-admin/www/auth/authority.tcl->ad_string_truncate packages/acs-admin/www/auth/batch-job.tcl packages/acs-admin/ www/auth/batch-job.tcl packages/acs-admin/www/auth/batch-job.tcl->ad_string_truncate

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 $string
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: