Skip to content Skip to sidebar Skip to footer

Cut Html Tags And Wrap Html Tags Again Part/2

its possible to map the cascade of tags in each child-tag of current parent tag, like this FROM:

string bold italic string

Solution 1:

Solution

source

$page = '
<u>
str
<b>
bold
<em>
ital
<strike>
ic stri
</strike>
ng
</em>
also(bold)
</b></u><u>
str
<b>
bold
also(bold)
</b></u>
';

Init vars ...

$o = 'open';
$c = 'close';
$n = 'node';

$tag = null;
$tagName = null;
$addNode = null;
$strTmp = null;

grep each lines ...

foreach(preg_split("/((\r?\n)|(\r\n?))/", $page) as$line){
    $lines[] =$line;
}

replace logic ...

for($i=0;$i<count($lines);$i++) {

    $line = $lines[$i];
    preg_match ('/<([^\/<]*?)>/' , $line, $open);
    preg_match ('/<(\/[^<]*?)>/' , $line, $close);

    $str = ""; 

    if($tag == $o ){

        if($addNode) {
            $str .= "#".$addNode."#";
        }
        $str .= $line;

        preg_match ('/<(\/[^<]*?)>/' , $lines[$i+1], $m);
        if(!strpos(@$m[1], $tagName)) {
            $addNode .= $tagName." ";
        }
    }

    if($tag == $c ){
        $str .= $line;
        $addNode = null;
    }

    if($tag == $n ){
        $str .= $line;
    }

    //$line = $addNode.$line;

    if(count($open)>0){$tag = $o; $tagName = $open[1];}
    if(count($close)>0){$tag = $c;}
    if(count($open)<1 && count($close)<1 ){$tag = $n;}

    $strTmp .= $str;

}

print ...

echo$strTmp = preg_replace('(<([\w]+)>#([^#]*)#)' , "<$1$2>", $strTmp);

Post a Comment for "Cut Html Tags And Wrap Html Tags Again Part/2"