function searchExcerpt($PlainText, $SearchTerms, $Length = 200, $Mark = true) { if (empty($SearchTerms)) { return substrWord($PlainText, 0, $Length); } if (is_string($SearchTerms)) { $SearchTerms = preg_split('`[\\s|-]+`i', $SearchTerms); } // Split the string into lines. $Lines = explode("\n", $PlainText); // Find the first line that includes a search term. foreach ($Lines as $i => &$Line) { $Line = trim($Line); if (!$Line) { continue; } foreach ($SearchTerms as $Term) { if (!$Term) { continue; } if (($Pos = mb_stripos($Line, $Term)) !== false) { $Line = substrWord($Line, $Term, $Length); if ($Mark) { return markString($SearchTerms, $Line); } else { return $Line; } } } } // No line was found so return the first non-blank line. foreach ($Lines as $Line) { if ($Line) { return sliceString($Line, $Length); } } return ''; }
function SearchExcerpt($PlainText, $SearchTerms, $Length = 200, $Mark = true) { if (empty($SearchTerms)) { return substrWord($PlainText, 0, $Length); } if (is_string($SearchTerms)) { $SearchTerms = preg_split('`[\\s|-]+`i', $SearchTerms); } // Split the string into lines. $Lines = explode("\n", $PlainText); // Find the first line that includes a search term. foreach ($Lines as $i => &$Line) { $Line = trim($Line); if (!$Line) { continue; } foreach ($SearchTerms as $Term) { if (!$Term) { continue; } if (($Pos = mb_stripos($Line, $Term)) !== FALSE) { $Line = substrWord($Line, $Term, $Length); // if ($Pos + mb_strlen($Term) > $Length) { // $St = -(strlen($Line) - ($Pos - $Length / 4)); // $Pos2 = strrpos($Line, ' ', $St); // if ($Pos2 !== FALSE) // $Line = '…'.substrWord($Line, $Pos2, $Length, "!!!"); // else // $Line = '…!'.mb_substr($Line, $St, $Length); // } else { // $Line = substrWord($Line, 0, $Length, '---'); // } return MarkString($SearchTerms, $Line); } } } // No line was found so return the first non-blank line. foreach ($Lines as $Line) { if ($Line) { return SliceString($Line, $Length); } } }