Created
January 6, 2016 17:20
-
-
Save Lweek/42169cdfca0b2e0f4026 to your computer and use it in GitHub Desktop.
PHP function to break long words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, '​', 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