Posted By

berkes on 09/07/07


Tagged

php String drupal theme interface accessability


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

basicmagic
digiloper


Drupal - Shorten a string - general API


 / Published in: PHP
 

URL: http://webschuur.com

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.

  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. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: deadcyclo on June 11, 2011

Why would you do this when you have the built in truncate_utf8 in Drupal?

You need to login to post a comment.