- str_split: documentation ( source)
<?php
function htmlWrapThing($str, $size){
$html = false;
$i = 0;
$chars = str_split($str);
$return = "";
$break = false;
$tag = "";
foreach($chars as $char){
if($char=="<") $html = true;
if($char==">") $html = false;
if($html) $t++;
if($html && $char!="/" && $t > 1){
$tag .= $char;
}
if(!$html) $i++;
$return .= $char;
if($i==$size) $break = true;
if($char == " " && !$html && $break){
if($oldtag != $tag){
$tag = "";
}
$return .= '</'.$tag.'>'."<br><br>".'<'.$tag.'>';
$i=0;
$oldtag = $tag;
$break = false;
}
}
return $return;
}
$str = "<h1>hilo everybody how is everyone doing tonight?</h1><p>hello world how is everyone doing today</p><p>hello world how is everyone doing today</p><p>hello world how is everyone doing today</p><br><br />hello everybody how are you doing today?";
echo htmlWrapThing($str,10);