Skip to content

Instantly share code, notes, and snippets.

@donwilson
Created May 10, 2017 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save donwilson/5e4557ece01c78af71b2989b50e095ef to your computer and use it in GitHub Desktop.
Save donwilson/5e4557ece01c78af71b2989b50e095ef to your computer and use it in GitHub Desktop.
Extract first X sentences from WordPress post content for post excerpt
<?php
/**
* Extract out first X sentences from uncleaned raw text
* @param string $text Raw text
* @param int $num_sentences Optional. Number of sentences
* @return string
*/
function extract_sentences_from_text($raw_text, $num_sentences=3) {
$text = $raw_text;
$text = strip_tags($text);
if(function_exists("strip_shortcodes")) {
$text = strip_shortcodes($text);
}
$text = preg_replace("#https?\://([^\s*]+)#si", " ", $text);
$text = str_replace("\r", "", $text);
$text = str_replace("\n", " ", $text);
$text = preg_replace("#\s{2,}#si", " ", $text);
$text = trim($text);
if("" === $text) {
return $text;
}
$lines = preg_split("#(\"?(?:\.|\!|\?)\"?\s+\"?[A-Z])#si", $text, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
if(!empty($lines)) {
$raw_sentences = array();
$last_letter = "";
foreach($lines as $key => $line) {
if(($key % 2) == 1) {
$raw_sentences[] = $last_letter . $lines[ ($key - 1) ] . trim(substr($line, 0, -1));
$last_letter = substr($line, -1);
}
if(count($raw_sentences) >= $num_sentences) {
break;
}
}
if(!empty($raw_sentences)) {
$sentences = array_slice($raw_sentences, 0, $num_sentences);
return implode(" ", $sentences);
}
}
return "";
}
// excerpt filter
add_filter('get_the_excerpt', function($excerpt) {
global $wpdb;
if(get_the_ID() && function_exists("extract_sentences_from_text") && ("" !== ($first_sentences = extract_sentences_from_text(get_the_content(), 3)))) {
return $first_sentences;
}
return $excerpt;
}, 1, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment