PageRenderTime 63ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Utils/StringHtmlUtils.class.php

https://gitlab.com/dlight/dlight
PHP | 120 lines | 83 code | 6 blank | 31 comment | 10 complexity | 04cb99222160680a701435d5115f4d89 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. /**
  3. * Copyright 2014 Didier Delhaye
  4. *
  5. * This file is part of DLight.
  6. * DLight is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. * DLight is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with DLight. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * @author Didier Delhaye <didier.delhaye@drkdidel.be>
  18. */
  19. namespace Library\Utils;
  20. class StringHtmlUtils
  21. {
  22. public function convertTagContentToHtmlentities($text, $tagName)
  23. {
  24. $convertedText = '';
  25. $startTag = "<$tagName>";
  26. $endTag = "</$tagName>";
  27. while (($positionStart = strpos($text, $startTag)) !== false) {
  28. /*
  29. * //after 1 loop, $text starts with </pre>
  30. * so we ignore it.
  31. */
  32. $positionEnd = strpos($text, $endTag, 1);
  33. $positionStart += strlen($startTag);
  34. $convertedText .= substr($text, 0, $positionStart);
  35. $convertedText .= htmlentities(substr($text, $positionStart, $positionEnd - $positionStart));
  36. $text = substr($text, $positionEnd);
  37. }
  38. $convertedText .= $text;
  39. return $convertedText;
  40. }
  41. private function stripTags($html, $tagName)
  42. {
  43. $startTag = "<$tagName>";
  44. $endTag = "</$tagName>";
  45. $positionStart = strpos($html, $startTag);
  46. $positionEnd = strpos($html, $endTag);
  47. return substr($html, $positionStart + strlen($startTag), $positionEnd - strlen($startTag));
  48. }
  49. private function deleteIncompleteTags($text)
  50. {
  51. return preg_replace('/<[^>]*$/', '', $text);
  52. }
  53. public function closeTags($text)
  54. {
  55. // Delete incomplete opening and closing tags, including auto-closing ones.
  56. $newtext = $this->deleteIncompleteTags($text);
  57. if (substr($newtext, 0, 1) !== '<') {
  58. $newtext = "<p>".$newtext;
  59. }
  60. // match all opening and closing tags
  61. // (?:...) tells regex to not capture the subpattern (override default behavior)
  62. // @source http://nl3.php.net/manual/en/regexp.reference.subpatterns.php
  63. preg_match_all('/<\/?[a-z]+(?: [a-z0-9]+="[^<>"]*")*>/', $newtext, $matches);
  64. $unclosedTags = array();
  65. foreach ($matches[0] as $key => $tag) {
  66. $tag = preg_replace('/</', '', $tag);
  67. $tag = preg_replace('/(>| .*)$/', '', $tag);
  68. $firstChar = substr($tag, 0, 1);
  69. if ($firstChar !== '/') {
  70. $unclosedTags[] = $tag;
  71. } else {
  72. $tag = substr($tag, 1);
  73. if (($key = array_search($tag, $unclosedTags)) !== false) {
  74. unset($unclosedTags[$key]);
  75. }
  76. }
  77. }
  78. $unclosedTags = array_reverse($unclosedTags);
  79. foreach ($unclosedTags as $key => $tag) {
  80. // Delete <a> tags rather than close them
  81. // because it makes no sense to leave a partial link.
  82. if ($tag == 'a') {
  83. $newtext = substr($newtext, 0, strrpos($newtext, '<a'));
  84. } else {
  85. // Delete the tag if it is empty (no text after the opening tag).
  86. $openingTag = "<$tag>";
  87. if (($openingTagPosition = strrpos($newtext, $openingTag)) < strlen($newtext) - strlen($openingTag)) {
  88. $newtext .= "</$tag>";
  89. } else {
  90. $newtext = substr($newtext, 0, $openingTagPosition);
  91. }
  92. }
  93. }
  94. return $newtext;
  95. }
  96. public function replacePlaceholdersWithHtmlTags($text, array $htmlTags)
  97. {
  98. $text = $this->deleteIncompleteTags($text);
  99. $placeholders = array();
  100. $tags = array();
  101. foreach ($htmlTags as $htmlTag) {
  102. $placeholders[] = $this->generatePlaceholder($htmlTag->position());
  103. $tags[] = $htmlTag->tag();
  104. }
  105. return str_replace($placeholders, $tags, $text);
  106. }
  107. public function generatePlaceholder($number)
  108. {
  109. return '<$'.sprintf('%02s', $number).'$>';
  110. }
  111. }