/** * UTF-8 aware alternative to stristr * * Returns all of haystack from the first occurrence of needle to the end. * needle and haystack are examined in a case-insensitive manner * Find first occurrence of a string using case insensitive comparison * * @param string the haystack * @param string the needle * @return string the sub string * @see http://www.php.net/stristr */ public static function stristr($str, $search) { if (strlen($search) == 0) { return $str; } $lstr = KHelperString::strtolower($str); $lsearch = KHelperString::strtolower($search); preg_match('|^(.*)' . preg_quote($lsearch) . '|Us', $lstr, $matches); if (count($matches) == 2) { return substr($str, strlen($matches[1])); } return FALSE; }
/** * returns substring of characters around a searchword. * * @param string The source string * @param int Number of chars to return * @param string The searchword to select around * * @return string */ public function substring($text, $searchword, $length = 200) { $textlen = KHelperString::strlen($text); $lsearchword = KHelperString::strtolower($searchword); $wordfound = false; $pos = 0; while ($wordfound === false && $pos < $textlen) { if (($wordpos = @KHelperString::strpos($text, ' ', $pos + $length)) !== false) { $chunk_size = $wordpos - $pos; } else { $chunk_size = $length; } $chunk = KHelperString::substr($text, $pos, $chunk_size); $wordfound = KHelperString::strpos(KHelperString::strtolower($chunk), $lsearchword); if ($wordfound === false) { $pos += $chunk_size + 1; } } //while if ($wordfound !== false) { return ($pos > 0 ? '... ' : '') . $chunk . ' ...'; } else { if (($wordpos = @KHelperString::strpos($text, ' ', $length)) !== false) { return KHelperString::substr($text, 0, $wordpos) . ' ...'; } else { return KHelperString::substr($text, 0, $length); } } }