Posted By

splotchy on 07/10/06


Tagged

trim smart


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tylerhall


smart_trim


 / Published in: PHP
 

  1. #################################################
  2. ####
  3. #### smart_trim
  4. #### This function trims a string to a specified length.
  5. #### Words are separated by space characters, and they are not
  6. #### chopped if possible.
  7. ####
  8. #### @package smart_trim
  9. #### @author Michael Gauthier <[email protected]>
  10. #### silverorange
  11. #### labs.silverorange.com
  12. ####
  13. #### Copyright (c) 2003, silverorange Inc.
  14. #### All rights reserved.
  15. ####
  16. #### Redistribution and use in source and binary forms, with or without modification,
  17. #### are permitted provided that the following conditions are met:
  18. ####
  19. #### * Redistributions of source code must retain the above copyright notice, this
  20. #### list of conditions and the following disclaimer.
  21. #### * Redistributions in binary form must reproduce the above copyright notice,
  22. #### this list of conditions and the following disclaimer in the documentation
  23. #### and/or other materials provided with the distribution.
  24. #### * Neither the name of silverorange Inc. nor the names of its contributors may
  25. #### be used to endorse or promote products derived from this software without
  26. #### specific prior written permission.
  27. ####
  28. #### THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  29. #### ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  30. #### WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  31. #### IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  32. #### INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. #### BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. #### DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. #### LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  36. #### OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  37. #### OF THE POSSIBILITY OF SUCH DAMAGE.
  38. ####
  39. #################################################
  40.  
  41. /**
  42.  * Trim the string.
  43.  *
  44.  * @param string $text The line to trim.
  45.  * @param integer $max_len The maximum length of the trimmed line.
  46.  * This ignores the length of the characters
  47.  * that indicate trimming has occured.
  48.  * @param boolean $trim_middle Trimming takes place in the middle of the line
  49.  * iff true. Otherwise, the line is trimmed at the
  50.  * end. Defaults to false.
  51.  * @param string $trim_chars Characters to use to indicate trimming has
  52.  * occured. Defaults to '...'.
  53.  *
  54.  * @return string The trimmed line of text.
  55.  */
  56. function smart_trim($text, $max_len, $trim_middle = false, $trim_chars = '...')
  57. {
  58. $text = trim($text);
  59.  
  60. if (strlen($text) < $max_len) {
  61.  
  62. return $text;
  63.  
  64. } elseif ($trim_middle) {
  65.  
  66. $hasSpace = strpos($text, ' ');
  67. if (!$hasSpace) {
  68. /**
  69. * The entire string is one word. Just take a piece of the
  70. * beginning and a piece of the end.
  71. */
  72. $first_half = substr($text, 0, $max_len / 2);
  73. $last_half = substr($text, -($max_len - strlen($first_half)));
  74. } else {
  75. /**
  76. * Get last half first as it makes it more likely for the first
  77. * half to be of greater length. This is done because usually the
  78. * first half of a string is more recognizable. The last half can
  79. * be at most half of the maximum length and is potentially
  80. * shorter (only the last word).
  81. */
  82. $last_half = substr($text, -($max_len / 2));
  83. $last_half = trim($last_half);
  84. $last_space = strrpos($last_half, ' ');
  85. if (!($last_space === false)) {
  86. $last_half = substr($last_half, $last_space + 1);
  87. }
  88. $first_half = substr($text, 0, $max_len - strlen($last_half));
  89. $first_half = trim($first_half);
  90. if (substr($text, $max_len - strlen($last_half), 1) == ' ') {
  91. /**
  92. * The first half of the string was chopped at a space.
  93. */
  94. $first_space = $max_len - strlen($last_half);
  95. } else {
  96. $first_space = strrpos($first_half, ' ');
  97. }
  98. if (!($first_space === false)) {
  99. $first_half = substr($text, 0, $first_space);
  100. }
  101. }
  102.  
  103. return $first_half.$trim_chars.$last_half;
  104.  
  105. } else {
  106.  
  107. $trimmed_text = substr($text, 0, $max_len);
  108. $trimmed_text = trim($trimmed_text);
  109. if (substr($text, $max_len, 1) == ' ') {
  110. /**
  111. * The string was chopped at a space.
  112. */
  113. $last_space = $max_len;
  114. } else {
  115. /**
  116. * In PHP5, we can use 'offset' here -Mike
  117. */
  118. $last_space = strrpos($trimmed_text, ' ');
  119. }
  120. if (!($last_space === false)) {
  121. $trimmed_text = substr($trimmed_text, 0, $last_space);
  122. }
  123. return remove_trailing_punctuation($trimmed_text).$trim_chars;
  124.  
  125. }
  126.  
  127. }
  128.  
  129. /**
  130.  * Strip trailing punctuation from a line of text.
  131.  *
  132.  * @param string $text The text to have trailing punctuation removed from.
  133.  *
  134.  * @return string The line of text with trailing punctuation removed.
  135.  */
  136. function remove_trailing_punctuation($text)
  137. {
  138. return preg_replace("'[^a-zA-Z_0-9]+$'s", '', $text);
  139. }

Report this snippet  

You need to login to post a comment.