text.php 3.0 KB

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