Return to Snippet

Revision: 3727
at September 7, 2007 09:05 by berkes


Initial Code
/**
 * Shorten a string
 **/
function format_shorten_string($string, $length = 15, $count_addition = 3) {
  if (drupal_strlen($string) > $length) {
    $out = theme('shorten_string', drupal_substr($string, 0, ($length - $count_addition)), $string, $count_addition);
  }
  else {
    $out = $string;
  }

  return $out;
}

/**
 * Function to theme the read more links
 * @ingroup themeable
 * @param $short_string The shortened string
 * @param $full_string The unshortened string, for display in the tooltip
 * @param $count_addition The amount of charanters that the calling function wants to be added. Defaults to three. Optional.
 */
function theme_shorten_string($short_string, $full_string, $count_addition = 3) {
  while ($i < $count_addition) {
    $i++;
    $addition .= '.';
  }

  return '<span title="'. $full_string .'">'. $short_string . $addition .'</span>';
}

Initial URL
http://webschuur.com

Initial Description
Shortens a string, and adds a span with a title of the full string. 
Function is useful for listings where you don't want wraping; or for places where a long string liek a username can break the layout.

Initial Title
Drupal - Shorten a string - general API

Initial Tags
php, drupal, theme

Initial Language
PHP