Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Code will trim a string to a maximum length, and will not break sentences (will grab as many sentences from text as it can up to the maximum string length).
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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment