Skip to content

Instantly share code, notes, and snippets.

@Lweek
Created January 6, 2016 17:20
Show Gist options
  • Save Lweek/42169cdfca0b2e0f4026 to your computer and use it in GitHub Desktop.
Save Lweek/42169cdfca0b2e0f4026 to your computer and use it in GitHub Desktop.
PHP function to break long words
<?php
/**
* Detect overly long words in text and add whitespaces into them to let them wrap
* @param string $text Text to be processed
* @return string
* @author Vladimir Belohradsky
*/
function breakLongWords($text) {
$maxWordLenght = 30;
$words = explode(' ',$text);
foreach ($words as $word) {
if (strlen($word) > $maxWordLenght) {
$fixedWord = wordwrap($word, $maxWordLenght, '&NegativeMediumSpace;', true);
$text = str_replace($word, $fixedWord, $text);
}
}
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment