开发者

Parse and re-write HTML with PHP

I am using Joomla 1.5 and have copied the mod_mainmenu to create a custom menu module. The menu has submenu items (inner UL tags). Here the menu's HTML:

<ul id="sub-menu">
    <li class="parent item10"><a href="link1.php"><span>Arts</span></a>
        <ul>
            <li class="item29">
                <a href="link2.php"><span>Arts Show</span></a></li>
            <开发者_如何学运维;li class="item29">
                <a href="link3.php"><span>Another Arts Show</span></a></li>
        </ul>
    </li>
    <li class="parent item15"><a href="link6.php"><span>News</span></a>
        <ul>
            <li class="item18">
                <a href="link7.php"><span>News Show</span></a></li>
        </ul>
    </li>
</ul>

I need to take the above HTML and rewrite it to look like this before it reaches the page:

<div id="sub-menu">
    <h3 class="parent item10"><a href="link1.php"><span>Arts</span></a></h3>
    <div>
       <a class="item29" href="link2.php"><span>Arts Show</span></a><br />
       <a class="item29" href="link3.php"><span>Another Arts Show</span></a>
    </div>
    <h3 class="parent item15"><a href="link6.php"><span>News</span></a></h3>
    <div>
       <a class="item18" href="link7.php"><span>News Show</span></a>
    </div>
</div>

How do you do this in PHP? Thanks.


Since jQuery UI Accordion don't handle nested list, you can use other jquery plugins that does (giyf).

If you want to modify html output from your duplicated mod_mainmenu, then simply locate/modify corresponding code (it should be in a view since joomla use mvc).


PHP has a Document Object Model class which can be used for this purpose.

However it would be a whole lot easier to just change the template that drives the output in Joomla and make the change at its source.


Here's a full script to do the job. It's in pure php.

<?php
    $dataStr = '<ul id="sub-menu">
    <li class="parent item10"><a href="link1.php"><span>Arts</span></a>
        <ul>
            <li class="item29">
                <a href="link2.php"><span>Arts Show</span></a></li>
            <li class="item29">
                <a href="link3.php"><span>Another Arts Show</span></a></li>
        </ul>
    </li>
    <li class="parent item15"><a href="link6.php"><span>News</span></a>
        <ul>
            <li class="item18">
                <a href="link7.php"><span>News Show</span></a></li>
        </ul>
    </li>
</ul>';


    $tempStr = str_replace('<ul ','<div ',$dataStr);
    $dataStr = str_replace('<li class="parent','<h3 class="parent ',$tempStr);
    $tempStr = str_replace('<ul>','<div>',$dataStr);
    $dataStr = str_replace('</ul>','</div>',$tempStr);
    $tempStr = str_replace('</li>','',$dataStr);
    $dataStr = str_replace('><a ','><a_keep ',$tempStr);
    $tempStr = str_replace('<a ','',$dataStr);
    $dataStr = str_replace('<li ','<a ',$tempStr);
    $tempStr = str_replace('><a_keep ','><a ',$dataStr);

    $dataStr = $tempStr;

    $newDataStr = "";
    $startIndex = 0;
    $endIndex = strpos($dataStr, "\n", $startIndex);
    $trimNextLine = false;

    while (1==1){
        if($endIndex == false){
            break;
        }

        $currentLine = substr($dataStr,$startIndex,$endIndex-$startIndex);
        $trimCurrentLine = ltrim($currentLine);
        $subStrCurrentLine = substr($trimCurrentLine,0,5);
        if($subStrCurrentLine==""){
            ;//do nothing
        }
        else if($subStrCurrentLine=="<h3 c"){
            $tempString = '    '.ltrim(substr($currentLine,0,strlen($currentLine)));
            $newDataStr = $newDataStr . $tempString . "</h3>\n";
        }
        else if($subStrCurrentLine=="<a cl"){
         $tempString = '        '.ltrim(substr($currentLine,0,strlen($currentLine)-1));
         $newDataStr = $newDataStr . $tempString;
         $trimNextLine = true;
        }
        else if($subStrCurrentLine=="<div>"){
         $tempString = '    '.ltrim(substr($currentLine,0,strlen($currentLine)));
         $newDataStr = $newDataStr . $tempString."\n";
         $trimNextLine = true;
        }
        else if($subStrCurrentLine=="</div"){
         $tempString = '    '.ltrim(substr($currentLine,0,strlen($currentLine)));
         $newDataStr = $newDataStr . $tempString."\n";
         $trimNextLine = true;
        }
        else{
            if ($trimNextLine == true){
                $trimNextLine = false;

                $nextStartIndex = $endIndex+1;
                $nextEndIndex = strpos($dataStr, "\n", $nextStartIndex);
                $nextLine = substr($dataStr,$nextStartIndex,$nextEndIndex-$nextStartIndex);
                $trimNextLine = ltrim($nextLine);
                $subStrNextLine = substr($trimNextLine,0,5);

                if($subStrNextLine=="<a cl"){
                    $newDataStr = $newDataStr . ' ' . ltrim($currentLine)."<br />\n";
                }
                else{
                    $newDataStr = $newDataStr . ' ' . ltrim($currentLine)."\n";
                }
            }
            else{
                $newDataStr = $newDataStr . $currentLine."\n";
            } 
        }

        $startIndex = $endIndex+1;
        $endIndex = strpos($dataStr, "\n", $startIndex);
    }

    $tempString = substr($dataStr,$startIndex,strlen($dataStr)-$startIndex);
    $newDataStr = $newDataStr . $tempString . "\n";

    echo($newDataStr);
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜