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

/lib/MiniMVC/MiniMVC/Helper/Text.php

https://github.com/tquensen/MiniMVC
PHP | 77 lines | 66 code | 9 blank | 2 comment | 8 complexity | 98f319edf666bfb4fcd38a3ec3ac2e6c MD5 | raw file
  1. <?php
  2. class Helper_Text extends MiniMVC_Helper
  3. {
  4. public function sanitize($input, $lower = false, $maxlength = false)
  5. {
  6. $list = $this->getReplaceList();
  7. $output = str_replace(array_keys($list), array_values($list), $input);
  8. $output = preg_replace('/[^A-Za-z0-9_-]+/', '-', $output);
  9. if ($maxlength) {
  10. $output = mb_substr($output, 0, $maxlength);
  11. }
  12. return $lower ? strtolower($output) : $output;
  13. }
  14. public function getReplaceList()
  15. {
  16. $replace = array(
  17. 'Ü' => 'Ue',
  18. 'Ö' => 'Oe',
  19. 'Ä' => 'Ae',
  20. 'ü' => 'ue',
  21. 'ö' => 'oe',
  22. 'ä' => 'ae',
  23. 'ß' => 'ss',
  24. '@' => '-at-'
  25. );
  26. return $replace;
  27. }
  28. public function truncate($content, $maxLength, $cutSize = 50,
  29. $insertAfter = '...')
  30. {
  31. if (mb_strlen($content, 'UTF-8') <= $maxLength) {
  32. return $content;
  33. }
  34. $content = mb_substr($content, 0, $maxLength);
  35. //try to cut after a sentence
  36. $LastCharSentence = array();
  37. $lastCharSentence['questionmark'] = mb_strrpos($content, '? ');
  38. $lastCharSentence['exclamationmark'] = mb_strrpos($content, '! ');
  39. $lastCharSentence['period'] = mb_strrpos($content, '. ');
  40. //or at least after a word
  41. $lastChar = array();
  42. $lastChar['space'] = mb_strrpos($content, ' ');
  43. $lastChar['newline'] = mb_strrpos($content, "\n");
  44. if (max($lastCharSentence) != 0 && (max($lastCharSentence) + 1) > ($maxLength - $cutSize)) {
  45. return mb_substr($content, 0, max($lastCharSentence) + 2) . $insertAfter;
  46. } elseif (max($lastChar) > $maxLength - $cutSize) {
  47. return mb_substr($content, 0, max($lastChar) + 1) . $insertAfter;
  48. } else {
  49. return $content . $insertAfter;
  50. }
  51. }
  52. public function esc($content, $print = true)
  53. {
  54. if ($print) {
  55. echo htmlspecialchars($content);
  56. return;
  57. }
  58. return htmlspecialchars($content);
  59. }
  60. public function raw($content, $print = true)
  61. {
  62. if ($print) {
  63. echo html_entity_decode($content, ENT_QUOTES, 'UTF-8');
  64. return;
  65. }
  66. return html_entity_decode($content, ENT_QUOTES, 'UTF-8');
  67. }
  68. }