TEXT.php 3.4 KB

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