Skip to content

Instantly share code, notes, and snippets.

@tannerhodges
Last active November 19, 2015 22:03
Show Gist options
  • Save tannerhodges/55501bdf669b007ce8d8 to your computer and use it in GitHub Desktop.
Save tannerhodges/55501bdf669b007ce8d8 to your computer and use it in GitHub Desktop.
Fix widows by adding an ` ` between the last `n` words of a string (defaults to the last 3 words).
// Courtesy of @cgutierrez
function fixWidows($text, $minWords = 3)
{
$words = preg_split('/\s+/', $text);
$textLength = count($words);
if (count($words) > $minWords) {
$widows = implode(' ', array_splice($words, ($minWords) * -1));
}
return trim(implode(' ', $words) . ' ' . $widows);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment