Crafting Excerpts in PHP or WordPress: Word, Sentence, and Paragraph Count Techniques

Creating excerpts in PHP is a common task in content management and website development. An excerpt is a shortened version of a longer piece of content, often used to provide a preview or summary. PHP developers might need to create excerpts based on word, sentence, or paragraph counts. This article explores methods to achieve this, along with best practices and handling cases where the count number exceeds the content length.

Excerpt by Word Count

Creating an excerpt by word count involves truncating the content after a certain number of words.

function excerptByWordCount($content, $wordCount) {
    $words = explode(' ', $content);
    if (count($words) > $wordCount) {
        $words = array_slice($words, 0, $wordCount);
        $content = implode(' ', $words);
    }
    return $content;
}

Usage:

// Excerpt of first 50 words
$wordCountExcerpt = excerptByWordCount($originalContent, 50); 

Best Practices and Handling Overcounts:

Excerpt by Sentence Count

Creating excerpts by sentence count involves keeping a certain number of sentences from the content.

function excerptBySentenceCount($content, $sentenceCount) {
    $sentences = explode('.', $content);
    if (count($sentences) > $sentenceCount) {
        $sentences = array_slice($sentences, 0, $sentenceCount);
        $content = implode('. ', $sentences) . '.';
    }
    return $content;
}

Usage

// Excerpt of first 3 sentences
$sentenceCountExcerpt = excerptBySentenceCount($originalContent, 3); 

To update the excerptBySentenceCount function to include sentences with any punctuation at the end (not just periods), you can modify the function to split the content by a regular expression that matches any typical sentence-ending punctuation, like a period, exclamation mark, or question mark. Here’s how you can do it in PHP:

function excerptBySentenceCount($content, $sentenceCount) {
    // Use a regular expression to split the content by sentence-ending punctuation
    $sentences = preg_split('/(?<=[.!?])\s+/', $content, -1, PREG_SPLIT_NO_EMPTY);

    if (count($sentences) > $sentenceCount) {
        $sentences = array_slice($sentences, 0, $sentenceCount);
        $content = implode(' ', $sentences);
        // Check the last character to ensure it ends with punctuation
        if (!preg_match('/[.!?]$/', $content)) {
            $content .= '.';
        }
    }
    return $content;
}

This function uses preg_split with a regular expression (regex) /(?<=[.!?])\s+/ which splits the text at spaces (\s+) that follow a period, exclamation mark, or question mark ([.!?]). The (?<=...) is a positive lookbehind assertion that checks for the presence of sentence-ending punctuation without including it in the split. The PREG_SPLIT_NO_EMPTY flag ensures that only non-empty pieces are returned.

Finally, the function checks if the last character of the resulting content is a sentence-ending punctuation. If not, it appends a period to maintain proper punctuation at the end of the excerpt.

Best Practices and Handling Overcounts:

Excerpt by Paragraph Count

Creating excerpts by paragraph count involves truncating the content after a certain number of paragraphs.

function excerptByParagraphCount($content, $paragraphCount) {
    $paragraphs = explode("\n", $content);
    if (count($paragraphs) > $paragraphCount) {
        $paragraphs = array_slice($paragraphs, 0, $paragraphCount);
        $content = implode("\n", $paragraphs);
    }
    return $content;
}

Usage:

// Excerpt of first 2 paragraphs
$paragraphCountExcerpt = excerptByParagraphCount($originalContent, 2); 

Best Practices and Handling Overcounts:

Excerpt by HTML Paragraph Count

When dealing with HTML content, you’ll want to extract excerpts based on the <p> tags to maintain the structure and formatting of the original content.

function excerptByHtmlParagraphCount($content, $paragraphCount) {
    preg_match_all('/<p[^>]*>.*?<\/p>/', $content, $paragraphs);
    $paragraphs = $paragraphs[0];

    if (count($paragraphs) > $paragraphCount) {
        $paragraphs = array_slice($paragraphs, 0, $paragraphCount);
        $content = implode(' ', $paragraphs);
    }
    return $content;
}

Usage:

// Excerpt of first 2 paragraphs
$paragraphCountExcerpt = excerptByHtmlParagraphCount($htmlContent, 2); 

Best Practices and Handling Overcounts:

Creating excerpts based on HTML paragraph count in PHP is a more advanced task compared to handling plain text. It’s essential to use regular expressions carefully to maintain the integrity of the HTML structure. This method is especially relevant for web applications where the content needs to be displayed with its original formatting. As always, validate the length of the original content and consider user experience when presenting excerpts.

Yes, WordPress has its own set of functions and features that facilitate creating excerpts, which can greatly simplify the process compared to manually handling excerpts in PHP. Here’s an overview of the key WordPress functions related to excerpts:

The Excerpt Function in WordPress

The WordPress API offers a robust system for handling excerpts, making manually implementing PHP functions unnecessary for most typical use cases. WordPress provides a user-friendly way to manage post summaries, whether it’s customizing the length, changing the read more text, or using template tags to display excerpts.

the_excerpt()

This WordPress template tag automatically prints an excerpt for a post. It’s commonly used in themes to display a post summary on archive pages.

get_the_excerpt()

This function retrieves the excerpt without displaying it, giving you more control over how and where to use it.

Customizing Excerpt Length

WordPress allows you to change the default excerpt length via the excerpt_length filter.

function custom_excerpt_length($length) {
    return 20; // Return 20 words as the new excerpt length
}
add_filter('excerpt_length', 'custom_excerpt_length');

Managing More Tag and Excerpt More Text

the_content('Read more')

This function displays the content until it encounters a “more” tag. It’s useful for showing a custom-length excerpt right within the content editor.

Customizing Excerpt More Text

You can customize the text that appears at the end of an excerpt (like […]) by using the excerpt_more filter.

function custom_excerpt_more($more) {
    return '...'; // Replace the default [...] with ...
}
add_filter('excerpt_more', 'custom_excerpt_more');

Handling HTML in Excerpts

WordPress excerpts are plain text by default. If you need to preserve HTML tags in excerpts, you must create a custom function or use a plugin designed for this purpose.

However, custom coding or plugins might be necessary for advanced requirements like preserving HTML tags in excerpts or creating excerpts based on specific elements like sentences or paragraphs.

Exit mobile version