Return to Snippet

Revision: 10197
at December 12, 2008 05:33 by DaveChild


Updated Code
function trim_text_in_sentences($intLength, $strText) {
    $intLastPeriodPos = strpos(strrev(substr($strText, 0, $intLength)), '.');
    if ($intLastPeriodPos === false) {
        $strReturn = substr($strText, 0, $intLength);
    } else {
        $strReturn = substr($strText, 0, ($intLength - $intLastPeriodPos));
    }
    return $strReturn;
}

Revision: 10196
at December 12, 2008 05:33 by DaveChild


Initial Code
function trim_text_in_sentences($intLength, $strText) {
    $intLastPeriodPos = strpos(strrev(substr($strText, 0, 200)), '.');
    if ($intLastPeriodPos === false) {
        $strReturn = substr($strText, 0, 200);
    } else {
        $strReturn = substr($strText, 0, (200 - $intLastPeriodPos));
    }
    return $strReturn;
}

Initial URL
http://www.addedbytes.com/code/

Initial Description
Pass in text and the maximum allowable length and this snippet will return as many full sentences from the text as it can within that length. If no sentences, it will just trim to the maximum length allowed.

Initial Title
PHP Get First Characters of String in Complete Sentences

Initial Tags
php

Initial Language
PHP