Alright I'm building a word wrapping function that will ignore tags like this [tag][/tag], and I suppose it could be used for the same purpose with html tags. For some reason it's moving the tags one short after I get passed the 2 line, I've got no clue why this is happening but then again I'm completely brain dead right now. If anyone here has a bit of time and would like to take a look to see if I've messed up the logic somewhere I'd really really appreciate your help. Thanks in advance.
function wrapwords($string, $chars=75, $separator=" <br>")
{
$tags = preg_replace("/(.*?)(\[.+?\])(.*?)/s", "$2,", $string);
$string2 = preg_replace("/(\[.+?\])/s", "", $string);
$string3 = wordwrap($string2, $chars, $separator, 1);
$len1 = strlen($string2);
$len2 = strlen($string3);
$tags = substr($tags, 0, strrpos($tags, ","));
$tags = explode(",", $tags);
$tags = array_unique($tags);
foreach($tags as $key => $value) {
$offset = 0;
$find = true;
while($find !== false) {
$find = strpos($string, $value, $offset);
if($find !== false) {
$replace[$find] = $value;
}
$offset = $find+1;
}
}
$lines = explode("<br>", $string3); // compares length of line to place tags accordingly
ksort($replace);
$length = 0; // length added by tags
$inc = 0; // shift in position due to tags added
$i = 1; // increments for new line
foreach($replace as $key => $value) {
$line = 0;
for($k=0;$k<$i;$k++) {
$line = ($line+strlen($lines[$k]));
}
if(($key-$length)>=($line)) {
$inc = ($inc + strlen($separator));
$i++;
}
$string3 = substr_replace($string3, $value, ($key+$inc), 0);
$length += strlen($value);
}
return $string3;
}
~ I suspect the problem has something to do with wordwrap removing spaces