PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/functions/text/truncate.php

https://bitbucket.org/Balancer/bors-core
PHP | 36 lines | 29 code | 7 blank | 0 comment | 8 complexity | 5d308be4dfeb6e33301db3d087e28f8f MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. function bors_truncate($string, $length = 80, $etc = NULL, $break_words = false, $middle = false)
  3. {
  4. if(is_null($etc))
  5. $etc = ec('…');
  6. return truncate($string, $length, $etc, $break_words, $middle);
  7. }
  8. function truncate($string, $length = 80, $etc = NULL, $break_words = false, $middle = false)
  9. {
  10. if($length == 0)
  11. return '';
  12. if(is_null($etc))
  13. $etc = ec('…');
  14. if(bors_strlen($string) <= $length)
  15. return $string;
  16. $length -= min($length, bors_strlen($etc));
  17. if(!$break_words && !$middle)
  18. {
  19. $string = preg_replace('/\s+?(\S+)?$/', '', bors_substr($string, 0, $length+1));
  20. }
  21. if(!$middle)
  22. {
  23. return bors_substr($string, 0, $length) . $etc;
  24. }
  25. else
  26. {
  27. return bors_substr($string, 0, $length/2) . $etc . bors_substr($string, -$length/2);
  28. }
  29. }