HTML_Helper.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * HTML helper file.
  4. *
  5. * Provides utilities to generate HTML content.
  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 IV
  10. */
  11. require_once(__DIR__ . "/Helper.php");
  12. /**
  13. * HTML helper.
  14. *
  15. * Contains utilities to generate HTML content.
  16. *
  17. * @category Helper.
  18. */
  19. final class HTML extends Helper{
  20. /**
  21. * This function closes all the opened HTML tags in a given string.
  22. *
  23. * @param string html The string with HTML tags.
  24. * @return string HTML with closed tags.
  25. */
  26. public static function close_tags($html) {
  27. $result = [];
  28. preg_match_all("#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU", $html, $result);
  29. $openedtags = $result[1];
  30. preg_match_all("#</([a-z]+)>#iU", $html, $result);
  31. $closedtags = $result[1];
  32. $len_opened = count($openedtags);
  33. if (count($closedtags) == $len_opened) {
  34. return $html;
  35. }
  36. $openedtags = array_reverse($openedtags);
  37. for ($i=0; $i < $len_opened; $i++) {
  38. if (!in_array($openedtags[$i], $closedtags)) {
  39. $html .= "</".$openedtags[$i].">";
  40. } else {
  41. unset($closedtags[array_search($openedtags[$i], $closedtags)]);
  42. }
  43. }
  44. return $html;
  45. }
  46. /**
  47. * Text shortener. Given a string, it trims in the proximity of the
  48. * desired string, up to the next white character. If indicated, it will
  49. * append a link to the full text.
  50. *
  51. * @param string $text The text to shorten.
  52. * @param int $length The desired length.
  53. * @param string $link_text Text for the link. Optional.
  54. * @param string $link URI of the link.
  55. * @return string Shortened text.
  56. */
  57. function cut_text($text, $length, $link_text = "", $link = ""){
  58. if (strlen($text) < $length){
  59. return $text;
  60. }
  61. $cut = substr($text, 0, strpos($text, " ", $length));
  62. $cut = close_tags($cut);
  63. if (strlen($cut) == 0){
  64. $cut = $text;
  65. }
  66. if (strlen($text) != strlen($cut) && strlen($link) > 0 && strlen($link_text) > 0){
  67. $cut = $cut . "... <a href='$link'>$link_text</a>";
  68. }
  69. return $cut;
  70. }
  71. /**
  72. * Creates the srcset attribute for content images.
  73. * Creates 9 different resolutions, from 100px to 900px.
  74. *
  75. * @param string $file Path to the file, relative to $path["img"]["content"]).
  76. * @return string srcset atttribute content.
  77. */
  78. public static function srcset($file){
  79. $srcset = "";
  80. $dir = dirname($file);
  81. $name = basename($file);
  82. foreach (range(1, 9) as $d) {
  83. $srcset = $srcset . URL::IMG["CONTENT"] . $dir . "/x" . $d . "00/" . $name . " " . $d . "00px, ";
  84. }
  85. $srcset = rtrim($srcset, ", ");
  86. return $srcset;
  87. }
  88. }