Skip to content

Instantly share code, notes, and snippets.

@DaveChild
Created July 24, 2009 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaveChild/154015 to your computer and use it in GitHub Desktop.
Save DaveChild/154015 to your computer and use it in GitHub Desktop.
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).
<?php
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