/**
* Clean duplicates new line
*
*/
function cleanNewLine($text) {
$newLine = "\r\n";
$strRes = '';
$posNewLine = strpos($text, $newLine);
if ($posNewLine===false) {
$strRes = $text;
} else {
$startText = substr($text, 0, $posNewLine+2);
$endText = substr($text, $posNewLine+2);
$strRes .= $startText."";
// Remove duplicate new line if exists
$posNewLine = strpos($endText, $newLine);
while($posNewLine===0) {
$endText = substr($endText, $posNewLine+2);
$posNewLine = strpos($endText, $newLine);
}
$strRes .= self::cleanNewLine($endText);
}
return $strRes;
}
Split long words in a text
Recursive php function, to split long words:
/**
* Cut words to max char, in a text
*
* @param string text Text to check
* @param integer maxChar Max word length
* @return string Verified text
*/
function checkWordLength($text, $maxChar=50) {
$arrText = explode(' ', $text);
foreach ($arrText as $key=>$t) {
if (strlen($t)>$maxChar) {
$arrText[$key] = chunk_split($t, $maxChar, ' ');
}
}
return implode(' ', $arrText);
}
Use:
$text = checkWordLength($text);