Widows Fix PHP or Javascript

Found this handy little article about how to fix widows on David Walsh Blog 

Using these simple preg_replace functions to stop the pesky run away words.

1
2
// With PHP
$text = preg_replace( '|([^\s])\s+([^\s]+)\s*$|', '$1 $2', $text);
1
2
// With JavaScript
var text = text.replace(/\s(?=[^\s]*$)/g, ' ');

Please check out our other posts with handy PHP snippets in Click Here