Drupal - Shorten a string - general API


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. /**
  2.  * Shorten a string
  3.  **/
  4. function format_shorten_string($string, $length = 15, $count_addition = 3) {
  5. if (drupal_strlen($string) > $length) {
  6. $out = theme('shorten_string', drupal_substr($string, 0, ($length - $count_addition)), $string, $count_addition);
  7. }
  8. else {
  9. $out = $string;
  10. }
  11.  
  12. return $out;
  13. }
  14.  
  15. /**
  16.  * Function to theme the read more links
  17.  * @ingroup themeable
  18.  * @param $short_string The shortened string
  19.  * @param $full_string The unshortened string, for display in the tooltip
  20.  * @param $count_addition The amount of charanters that the calling function wants to be added. Defaults to three. Optional.
  21.  */
  22. function theme_shorten_string($short_string, $full_string, $count_addition = 3) {
  23. while ($i < $count_addition) {
  24. $i++;
  25. $addition .= '.';
  26. }
  27.  
  28. return '<span title="'. $full_string .'">'. $short_string . $addition .'</span>';
  29. }

URL: http://webschuur.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.