function extrp_get_excerpt($post_id, $maxchars = '', $q = '', $highlight = '', $snippet = '') { $post_id = null === $post_id ? get_the_ID() : (int) $post_id; if ($snippet) { $args = array('post_id' => $post_id, 'q' => $q, 'highlight' => $highlight, 'maxchars' => $maxchars); $extrp_excerpt = extrp_excerpt($args); $excerpt = $extrp_excerpt->get(); } else { $excerpt = extrp_max_charlength($maxchars, strip_shortcodes(get_the_excerpt())); } return $excerpt; }
protected function highlight_excerpt() { $text = wp_strip_all_tags($this->content); for ($i = 0; $i < sizeof($this->keys); $i++) { $this->keys[$i] = preg_quote($this->keys[$i], '/'); } $workkeys = $this->keys; $ranges = array(); $included = array(); $length = 0; while (256 > $length && count($workkeys)) { foreach ($workkeys as $k => $key) { if (0 == strlen($key)) { unset($workkeys[$k]); continue; } if (256 <= $length) { break; } if (!isset($included[$key])) { $included[$key] = 0; } if (preg_match('/' . $key . '/iu', $text, $match, PREG_OFFSET_CAPTURE, $included[$key])) { $p = $match[0][1]; $success = 0; if (false !== ($q = strpos($text, ' ', max(0, $p - 60))) && $q < $p) { $end = substr($text, $p, 80); if (false !== ($s = strrpos($end, ' ')) && 0 < $s) { $ranges[$q] = $p + $s; $length += $p + $s - $q; $included[$key] = $p + 1; $success = 1; } } if (!$success) { $q = $this->_jamul_find_1stbyte($text, max(0, $p - 60)); $q = $this->_jamul_find_delimiter($text, $q); $s = $this->_jamul_find_1stbyte_reverse($text, $p + 80, $p); $s = $this->_jamul_find_delimiter($text, $s); if ($s >= $p && $q <= $p) { $ranges[$q] = $s; $length += $s - $q; $included[$key] = $p + 1; } else { unset($workkeys[$k]); } } } else { unset($workkeys[$k]); } } } if (0 == sizeof($ranges)) { return '<p>' . $this->_jamul_truncate($text, $this->maxchars) . ' ...</p>'; } ksort($ranges); $newranges = array(); foreach ($ranges as $from2 => $to2) { if (!isset($from1)) { $from1 = $from2; $to1 = $to2; continue; } if ($from2 <= $to1) { $to1 = max($to1, $to2); } else { $newranges[$from1] = $to1; $from1 = $from2; $to1 = $to2; } } $newranges[$from1] = $to1; $out = array(); foreach ($newranges as $from => $to) { $out[] = substr($text, $from, $to - $from); } $text = (isset($newranges[0]) ? '' : '... ') . implode(' ... ', $out) . ' ...'; //maxchars $text = extrp_max_charlength($this->maxchars, $text); $text = preg_replace('/(' . implode('|', $this->keys) . ')/iu', $this->highlight, $text); return "<p>{$text}</p>"; }