How can I cut a string in PHP if there's a certain amount of newlines? For example, say the string was 10 lines of text, all on a new line, how could I cut that string so that only the first 5 lines showed? I already cut the string to 650 characters, however, I'd also like to be able to cut it, should it reach too many newlines.
Current code that cuts the string if it's above 650 characters:
$string = $f2;
$maxLength = 650;
if (strlen($string) > $maxLength) {
$stringCut = substr($string, 0, $maxLength);
$string = substr($stringCut, 0, strrpos($stringCut, ' '));
}
$stringchar=htmlspecialchars($string);
echo nl2br($stringchar);