|
|
Ok!
|
|
|
Ok!
|
|---|---|---|---|---|
| 150 | function find_wrap($string, $search){
$out = "";
$pos = 0;
if(strpos(" " . $string, $search) > 0){
$in = str_replace($search, "<b>".$search."</b>", strip_tags($string));
$wordToFind = $search;
$numWordsBefore = 3;
$numWordsAfter = 10;
$words = preg_split('/s+/', $in);
$found_words = preg_grep("/^".$wordToFind.".*/", $words);
$found_pos = array_keys($found_words);
if(count($found_pos))
{
$pos = $found_pos[0];
}
if (isset($pos)) {
$start = ($pos - $numWordsBefore > 0) ? $pos - $numWordsBefore : 0;
$length = (($pos + ($numWordsAfter + 1) < count($words)) ? $pos + ($numWordsAfter + 1) : count($words) - 1) - $start;
$slice = array_slice($words, $start, $length);
$pre_start = ($start > 0) ? "...":"";
$post_end = ($pos + ($numWordsAfter + 1) < count($words)) ? "...":"";
$out = $pre_start.implode(' ', $slice).$post_end;
}
}
return $out;
} | wrap, фраза текста, поиск | 3400 | Возвращает искомое слово из текста плюс 3 слова до и 10 слов после |