/** * Function to trim text around a word or phrase * * @param string $haystack Text * @param string $needle Phrase * @return string */ public static function truncateTextAroundPhrase($haystack, $needle) { /* Base on words */ $haystack = explode(" ", $haystack); if (count($haystack) > 21) { $_term_at = IPSLib::arraySearchLoose($needle, $haystack); if ($_term_at - 11 > 0) { $begin = array_splice($haystack, 0, $_term_at - 11); /* The term position will have changed now */ $_term_at = IPSLib::arraySearchLoose($needle, $haystack); } if ($_term_at + 11 < count($haystack)) { $end = array_splice($haystack, $_term_at + 11, count($haystack)); } } else { $begin = array(); $end = array(); } $haystack = implode(" ", $haystack); if (is_array($begin) && count($begin)) { $haystack = '...' . $haystack; } if (is_array($end) && count($end)) { $haystack = $haystack . '...'; } return $haystack; }