TEXT_Helper.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Text helper file.
  4. *
  5. * Provides a helper to perform text operations.
  6. *
  7. * @category Helper.
  8. */
  9. /**
  10. * Require dependent files if not present.
  11. */
  12. require_once(PATH::HELPER . "Helper.php");
  13. /**
  14. * Text helper.
  15. *
  16. * Contains utilities to format and escapee text.
  17. *
  18. * @category Helper.
  19. */
  20. final class TEXT extends Helper{
  21. /**
  22. * Turns a date string into a human readable string on the specified
  23. * language. Only works for spanish, basque and english.
  24. *
  25. * @param string $str_date Date string ('yyyy-mm-dd' or 'yyyy-mm-dd HH:MM:SS')
  26. * @param string $time Append the time at the end.
  27. * @return string Formatted date string.
  28. */
  29. public static function format_date($str_date, $time = true){
  30. $date = strtotime($str_date);
  31. $year = date("o", $date);
  32. $month = date("n", $date);
  33. $month --;
  34. $day = date("j", $date);
  35. $wday = date("N", $date);
  36. $wday --;
  37. $hour = date("H", $date);
  38. $minute = date("i", $date);
  39. $week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
  40. $months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  41. if ($time){
  42. $str = "$week[$wday], $months[$month] $day, $year at $hour:$minute";
  43. }
  44. else{
  45. $str = "$week[$wday], $months[$month] $day, $year";
  46. }
  47. return $str;
  48. }
  49. /**
  50. * Closes all the opened HTML tags in a given string.
  51. *
  52. * @param string html The string with HTML tags.
  53. * @return string HTML with closed tags.
  54. */
  55. public static function close_tags($html) {
  56. $result = [];
  57. preg_match_all("#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU", $html, $result);
  58. $openedtags = $result[1];
  59. preg_match_all("#</([a-z]+)>#iU", $html, $result);
  60. $closedtags = $result[1];
  61. $len_opened = count($openedtags);
  62. if (count($closedtags) == $len_opened) {
  63. return $html;
  64. }
  65. $openedtags = array_reverse($openedtags);
  66. for ($i=0; $i < $len_opened; $i++) {
  67. if (!in_array($openedtags[$i], $closedtags)) {
  68. $html .= "</".$openedtags[$i].">";
  69. } else {
  70. unset($closedtags[array_search($openedtags[$i], $closedtags)]);
  71. }
  72. }
  73. return $html;
  74. }
  75. /**
  76. * Text shortener.
  77. *
  78. * Given a string, it trims in the proximity of the
  79. * desired string, up to the next white character. If indicated, it will
  80. * append a link to the full text.
  81. *
  82. * @param string $text The text to shorten.
  83. * @param int $length The desired length.
  84. * @param string $link_text Text for the link. Optional.
  85. * @param string $link URI of the link.
  86. * @return string Shortened text.
  87. */
  88. public static function cut_text($text, $length, $link_text = "", $link = ""){
  89. if (strlen($text) < $length){
  90. return $text;
  91. }
  92. $cut = substr($text, 0, strpos($text, " ", $length));
  93. $cut = close_tags($cut);
  94. if (strlen($cut) == 0){
  95. $cut = $text;
  96. }
  97. if (strlen($text) != strlen($cut) && strlen($link) > 0 && strlen($link_text) > 0){
  98. $cut = $cut . "... <a href='$link'>$link_text</a>";
  99. }
  100. return $cut;
  101. }
  102. }
  103. ?>