PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/joomla/html/html/string.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 155 lines | 74 code | 19 blank | 62 comment | 12 complexity | 5bb3c11479660b4d4fe5c15a178ce0c2 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage HTML
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * HTML helper class for rendering manipulated strings.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage HTML
  15. * @since 11.1
  16. */
  17. abstract class JHtmlString
  18. {
  19. /**
  20. * Truncates text blocks over the specified character limit and closes
  21. * all open HTML tags. The method will optionally not truncate an individual
  22. * word, it will find the first space that is within the limit and
  23. * truncate at that point. This method is UTF-8 safe.
  24. *
  25. * @param string $text The text to truncate.
  26. * @param integer $length The maximum length of the text.
  27. * @param boolean $noSplit Don't split a word if that is where the cutoff occurs (default: true).
  28. * @param boolean $allowHtml Allow HTML tags in the output, and close any open tags (default: true).
  29. *
  30. * @return string The truncated text.
  31. *
  32. * @since 11.1
  33. */
  34. public static function truncate($text, $length = 0, $noSplit = true, $allowHtml = true)
  35. {
  36. // Check if HTML tags are allowed.
  37. if (!$allowHtml)
  38. {
  39. // Deal with spacing issues in the input.
  40. $text = str_replace('>', '> ', $text);
  41. $text = str_replace(array('&nbsp;', '&#160;'), ' ', $text);
  42. $text = JString::trim(preg_replace('#\s+#mui', ' ', $text));
  43. // Strip the tags from the input and decode entities.
  44. $text = strip_tags($text);
  45. $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
  46. // Remove remaining extra spaces.
  47. $text = str_replace('&nbsp;', ' ', $text);
  48. $text = JString::trim(preg_replace('#\s+#mui', ' ', $text));
  49. }
  50. // Truncate the item text if it is too long.
  51. if ($length > 0 && JString::strlen($text) > $length)
  52. {
  53. // Find the first space within the allowed length.
  54. $tmp = JString::substr($text, 0, $length);
  55. if ($noSplit)
  56. {
  57. $offset = JString::strrpos($tmp, ' ');
  58. if (JString::strrpos($tmp, '<') > JString::strrpos($tmp, '>'))
  59. {
  60. $offset = JString::strrpos($tmp, '<');
  61. }
  62. $tmp = JString::substr($tmp, 0, $offset);
  63. // If we don't have 3 characters of room, go to the second space within the limit.
  64. if (JString::strlen($tmp) > $length - 3)
  65. {
  66. $tmp = JString::substr($tmp, 0, JString::strrpos($tmp, ' '));
  67. }
  68. }
  69. if ($allowHtml)
  70. {
  71. // Put all opened tags into an array
  72. preg_match_all("#<([a-z][a-z0-9]*)\b.*?(?!/)>#i", $tmp, $result);
  73. $openedTags = $result[1];
  74. $openedTags = array_diff($openedTags, array("img", "hr", "br"));
  75. $openedTags = array_values($openedTags);
  76. // Put all closed tags into an array
  77. preg_match_all("#</([a-z]+)>#iU", $tmp, $result);
  78. $closedTags = $result[1];
  79. $numOpened = count($openedTags);
  80. // All tags are closed
  81. if (count($closedTags) == $numOpened)
  82. {
  83. return $tmp . '...';
  84. }
  85. $openedTags = array_reverse($openedTags);
  86. // Close tags
  87. for ($i = 0; $i < $numOpened; $i++)
  88. {
  89. if (!in_array($openedTags[$i], $closedTags))
  90. {
  91. $tmp .= "</" . $openedTags[$i] . ">";
  92. }
  93. else
  94. {
  95. unset($closedTags[array_search($openedTags[$i], $closedTags)]);
  96. }
  97. }
  98. }
  99. $text = $tmp . '...';
  100. }
  101. return $text;
  102. }
  103. /**
  104. * Abridges text strings over the specified character limit. The
  105. * behavior will insert an ellipsis into the text replacing a section
  106. * of variable size to ensure the string does not exceed the defined
  107. * maximum length. This method is UTF-8 safe.
  108. *
  109. * For example, it transforms "Really long title" to "Really...title".
  110. *
  111. * Note that this method does not scan for HTML tags so will potentially break them.
  112. *
  113. * @param string $text The text to abridge.
  114. * @param integer $length The maximum length of the text (default is 50).
  115. * @param integer $intro The maximum length of the intro text (default is 30).
  116. *
  117. * @return string The abridged text.
  118. *
  119. * @since 11.1
  120. */
  121. public static function abridge($text, $length = 50, $intro = 30)
  122. {
  123. // Abridge the item text if it is too long.
  124. if (JString::strlen($text) > $length)
  125. {
  126. // Determine the remaining text length.
  127. $remainder = $length - ($intro + 3);
  128. // Extract the beginning and ending text sections.
  129. $beg = JString::substr($text, 0, $intro);
  130. $end = JString::substr($text, JString::strlen($text) - $remainder);
  131. // Build the resulting string.
  132. $text = $beg . '...' . $end;
  133. }
  134. return $text;
  135. }
  136. }