Created
July 24, 2009 11:27
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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