Skip to content

Instantly share code, notes, and snippets.

@nephthys
Created November 4, 2011 15:31
Show Gist options
  • Save nephthys/1339594 to your computer and use it in GitHub Desktop.
Save nephthys/1339594 to your computer and use it in GitHub Desktop.
Truncate paragraphs in PHP
<?php
function truncate_paragraphs($text, $limit) {
preg_match_all('/<p>(.*)<\/p>/', $text, $matches);
$paragraphs = $matches[0];
if (count($paragraphs) > $limit) {
$paragraphs = array_slice($paragraphs, 0, $limit);
return implode("\n", $paragraphs);
} else {
return $text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment