使用PHP解析并重写HTML

时间:2011-03-18 14:28:58

标签: php html xml joomla

我正在使用Joomla 1.5并复制mod_mainmenu以创建自定义菜单模块。菜单包含子菜单项(内部UL标签)。这里是菜单的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>

我需要使用上面的HTML并在它到达页面之前将其重写为:

<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>

你如何用PHP做到这一点?感谢。

3 个答案:

答案 0 :(得分:1)

PHP有一个Document Object Model类,可以用于此目的。

然而,改变驱动Joomla输出的模板并在其源头进行更改将会更加轻松

答案 1 :(得分:1)

由于jQuery UI Accordion不处理嵌套列表,你可以使用其他jquery插件(giyf)。

如果你想修改你复制的mod_mainmenu的html输出,那么只需找到/修改相应的代码(它应该在视图中,因为joomla使用mvc)。

答案 2 :(得分:1)

这是一个完整的脚本来完成这项工作。这是纯粹的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);
?>