/** * Marks up the search words from $this->sWarr in the $str with a color. * * @param string Text in which to find and mark up search words. This text is assumed to be UTF-8 like the search words internally is. * @return string Processed content. * @todo Define visibility */ public function markupSWpartsOfString($str) { // Init: $str = str_replace(' ', ' ', \TYPO3\CMS\Core\Html\HtmlParser::bidir_htmlspecialchars($str, -1)); $str = preg_replace('/\\s\\s+/', ' ', $str); $swForReg = array(); // Prepare search words for regex: foreach ($this->sWArr as $d) { $swForReg[] = preg_quote($d['sword'], '/'); } $regExString = '(' . implode('|', $swForReg) . ')'; // Split and combine: $parts = preg_split('/' . $regExString . '/i', ' ' . $str . ' ', 20000, PREG_SPLIT_DELIM_CAPTURE); // Constants: $summaryMax = 300; $postPreLgd = 60; $postPreLgd_offset = 5; $divider = ' ... '; $occurencies = (count($parts) - 1) / 2; if ($occurencies) { $postPreLgd = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($summaryMax / $occurencies, $postPreLgd, $summaryMax / 2); } // Variable: $summaryLgd = 0; $output = array(); // Shorten in-between strings: foreach ($parts as $k => $strP) { if ($k % 2 == 0) { // Find length of the summary part: $strLen = $GLOBALS['TSFE']->csConvObj->strlen('utf-8', $parts[$k]); $output[$k] = $parts[$k]; // Possibly shorten string: if (!$k) { // First entry at all (only cropped on the frontside) if ($strLen > $postPreLgd) { $output[$k] = $divider . preg_replace('/^[^[:space:]]+[[:space:]]/', '', $GLOBALS['TSFE']->csConvObj->crop('utf-8', $parts[$k], -($postPreLgd - $postPreLgd_offset))); } } elseif ($summaryLgd > $summaryMax || !isset($parts[$k + 1])) { // In case summary length is exceed OR if there are no more entries at all: if ($strLen > $postPreLgd) { $output[$k] = preg_replace('/[[:space:]][^[:space:]]+$/', '', $GLOBALS['TSFE']->csConvObj->crop('utf-8', $parts[$k], $postPreLgd - $postPreLgd_offset)) . $divider; } } else { // In-between search words: if ($strLen > $postPreLgd * 2) { $output[$k] = preg_replace('/[[:space:]][^[:space:]]+$/', '', $GLOBALS['TSFE']->csConvObj->crop('utf-8', $parts[$k], $postPreLgd - $postPreLgd_offset)) . $divider . preg_replace('/^[^[:space:]]+[[:space:]]/', '', $GLOBALS['TSFE']->csConvObj->crop('utf-8', $parts[$k], -($postPreLgd - $postPreLgd_offset))); } } $summaryLgd += $GLOBALS['TSFE']->csConvObj->strlen('utf-8', $output[$k]); // Protect output: $output[$k] = htmlspecialchars($output[$k]); // If summary lgd is exceed, break the process: if ($summaryLgd > $summaryMax) { break; } } else { $summaryLgd += $GLOBALS['TSFE']->csConvObj->strlen('utf-8', $strP); $output[$k] = '<strong class="tx-indexedsearch-redMarkup">' . htmlspecialchars($parts[$k]) . '</strong>'; } } // Return result: return implode('', $output); }