/components/com_search/templates/helpers/string.php

https://github.com/raeldc/nooku-server · PHP · 153 lines · 94 code · 20 blank · 39 comment · 19 complexity · 3a1cbfc1cb539a889a33fa34c545c685 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: select.php 1309 2011-05-17 16:47:27Z johanjanssens $
  4. * @category Nooku
  5. * @package Nooku_Server
  6. * @subpackage Search
  7. * @copyright Copyright (C) 2011 Timble CVBA and Contributors. (http://www.timble.net)
  8. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  9. * @link http://www.nooku.org
  10. */
  11. /**
  12. * String Template Helper Class
  13. *
  14. * @author Arunas Mazeika <http://nooku.assembla.com/profile/amazeika>
  15. * @category Nooku
  16. * @package Nooku_Server
  17. * @subpackage Search
  18. */
  19. class ComSearchTemplateHelperString extends KTemplateHelperAbstract
  20. {
  21. /**
  22. * Returns a summary of a text, highlights and generated a substring by default
  23. *
  24. * @param array An optional array with configuration options
  25. * @return string
  26. */
  27. public function summary($config)
  28. {
  29. $config = new KConfig($config);
  30. $config->append(array(
  31. 'text' => '',
  32. 'term' => '',
  33. 'match' => 'exact',
  34. 'lenght' => 200,
  35. 'highlight' => true,
  36. 'substring' => true
  37. ));
  38. if($config->match != 'exact')
  39. {
  40. $words = preg_split('/\s+/u', $config->term);
  41. $needle = $words[0];
  42. }
  43. else $needle = $config->term;
  44. // Strips tags won't remove the actual jscript
  45. $config->text = preg_replace( "'<script[^>]*>.*?</script>'si", "", $config->text );
  46. $config->text = preg_replace( '/{.+?}/', '', $config->text);
  47. // Replace line breaking tags with whitespace
  48. $config->text = preg_replace( "'<(br[^/>]*?/|hr[^/>]*?/|/(div|h[1-6]|li|p|td))>'si", ' ', $config->text );
  49. //Return a substring of characters around the term
  50. if($config->substring) {
  51. $config->text = $this->substring($config);
  52. }
  53. if($config->highlight) {
  54. $config->text = $this->highlight($config);
  55. }
  56. return $config->text;
  57. }
  58. /**
  59. * Returns a highlighted string based on a term
  60. *
  61. * @param array An optional array with configuration options
  62. * @return string
  63. */
  64. public function highlight($config)
  65. {
  66. $config = new KConfig($config);
  67. $config->append(array(
  68. 'text' => '',
  69. 'term' => '',
  70. 'match' => 'exact',
  71. ));
  72. if($config->match == 'exact') {
  73. $words = array($config->term);
  74. } else {
  75. $words = preg_split('/\s+/u', $config->term);
  76. }
  77. // Highlight search words
  78. $words = array_unique($words);
  79. $hlregex = '#(';
  80. $x = 0;
  81. foreach($words as $k => $hlword)
  82. {
  83. $hlregex .= ($x == 0 ? '' : '|');
  84. $hlregex .= preg_quote($hlword, '#');
  85. $x++;
  86. }
  87. $hlregex .= ')#iu';
  88. $text = preg_replace($hlregex, '<span class="highlight">\0</span>', $config->text);
  89. return $text;
  90. }
  91. /**
  92. * Returns substring of characters around a term
  93. *
  94. * @param array An optional array with configuration options
  95. * @return string
  96. */
  97. public function substring($config)
  98. {
  99. $config = new KConfig($config);
  100. $config->append(array(
  101. 'text' => '',
  102. 'term' => '',
  103. 'lenght' => 200,
  104. ));
  105. $textlen = JString::strlen($config->text);
  106. $lsearchword = JString::strtolower($config->term);
  107. $found = false;
  108. $pos = 0;
  109. $config->text = strip_tags( $config->text );
  110. while ($found === false && $pos < $textlen)
  111. {
  112. if (($wordpos = @JString::strpos($config->text, ' ', $pos + $config->lenght)) !== false) {
  113. $chunk_size = $wordpos - $pos;
  114. } else {
  115. $chunk_size = $config->lenght;
  116. }
  117. $chunk = JString::substr($config->text, $pos, $chunk_size);
  118. $found = JString::strpos(JString::strtolower($chunk), $lsearchword);
  119. if ($found === false) {
  120. $pos += $chunk_size + 1;
  121. }
  122. }
  123. if ($found == false)
  124. {
  125. if (($wordpos = @JString::strpos($config->text, ' ', $config->lenght)) !== false) {
  126. return JString::substr($config->text, 0, $wordpos) . '&nbsp;...';
  127. } else {
  128. return JString::substr($config->text, 0, $config->lenght);
  129. }
  130. }
  131. else return (($pos > 0) ? '...&nbsp;' : '') . $chunk . '&nbsp;...';
  132. }
  133. }