Skip to content

Instantly share code, notes, and snippets.

@BinaryMoon
Last active April 6, 2017 08:06
Show Gist options
  • Save BinaryMoon/7f8f6f1e9d111f3650cb504ee92e8f27 to your computer and use it in GitHub Desktop.
Save BinaryMoon/7f8f6f1e9d111f3650cb504ee92e8f27 to your computer and use it in GitHub Desktop.
Remove Widows from text in WordPress
/**
* Make last space in a sentence a non breaking space to prevent typographic widows.
*
* @param type $str
* @return string
*/
function theme_widont( $str = '' ) {
// Strip spaces.
$str = trim( $str );
// Find the last space.
$space = strrpos( $str, ' ' );
// If there's a space then replace the last on with a non breaking space.
if ( false !== $space ) {
$str = substr( $str, 0, $space ) . ' ' . substr( $str, $space + 1 );
}
// Return the string.
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment