I wrote this function earlier this year for someone else. What it does is wordwraps any word that is longer than your specified character length, without wrapping the rest of them. Comes in pretty handy. You could just use this, and set the $iMax variable to a very long enough value, or you could use something like a preg_split() on the <img> tag to split the text up into an array, then your even keys of the array will be text, and your odd keys will be the img links. You could even combine the 2 methods! So many possibilities 🙂
function split_words($sInput, $sDelim="\n", $iMax=20) {
$aWords = explode(" ", $sInput);
// Loop through the array created by explode
foreach($aWords as $sLongWord) {
// Check the length of the word
if(strlen($sLongWord) > $iMax) {
// If this ISN'T the first word put in a newline before the long word
if(!empty($sOutput)) {
$sOutput .= $sDelim;
}
// Loop through the word itself, splitting it up using substr
for($i=0; ($i*$iMax) < strlen($sLongWord); $i++) {
// the offset is needed to get the right part of the word
$iOffset = $i*$iMax;
$sOutput .= substr($sLongWord, $iOffset, $iMax).$sDelim;
}
// Else, just display the word with a space before hand (explode will strip the spaces)
} else {
$sOutput .= " ".$sLongWord;
}
}
return $sOutput;
}