简体   繁体   English

用PHP解析HTML以添加类名

[英]Parsing HTML with PHP To Add Class Names

I'm trying to figure out a way to add class names to an html output of mine with PHP. 我试图找出一种方法,用PHP将类名添加到我的html输出中。

The inputs: 输入:

<div class="clsA">
  <div>
    <div class="clsB">
      Content (can be anything including clsA, clsB etc.)
    </div>
  </div>
</div>

or 要么

<div class="clsC">
  <div class="clsB">
      Content (can be anything including clsC, clsB etc.)
  </div>
</div>

and I want the output as 我希望输出为

<div class="clsA myClass">
  <div>
    <div class="clsB clsInner">
      Content (can be anything including clsA, clsB etc.)
    </div>
  </div>
</div>

<div class="clsC myClass">
  <div class="clsB clsInner">
      Content (can be anything including clsC, clsB etc.)
  </div>
</div>

For the root class, I want to insert a class name and for a second div with a specified class name, I want to add another class name. 对于根类,我要插入一个类名,对于具有指定类名的第二个div,我想添加另一个类名。 But I couldn't figure out a way to add them without using a third party library. 但是我想不办法不使用第三方库就添加它们。

Basically as Pseudo code 基本上是伪代码

- Add myClass to the root element
- Add clsInner to the first child element of the root with class name clsB.

Note: I can't replace clsB with "clsB clsInner", since there are other classes with clsB inside the content. 注意:我不能用“ clsB clsInner”替换clsB,因为内容中还有其他带有clsB的类。

Not too familiar with PHP, but would this work? 对PHP不太熟悉,但这行得通吗?

$dom = new DOMDocument;
$dom->loadHTML($html);
$divs = $dom->getElementsByTagName('div');
foreach ($divs as $div) {
        $div->setAttribute('class', $div->getAttribute('class').' myclass');
}
$html = $dom->saveHTML();

As I know, to get an element by class with DOMDocument , we need to use also DOMXpath() . 据我所知,要使用DOMDocument通过类获取元素,我们还需要使用DOMXpath()

So you may try this: 因此,您可以尝试以下操作:

<?php
$input = '<div class="clsA">
  <div>
    <div class="clsB">
      Content (can be anything including clsA, clsB etc.)
    </div>
  </div>
</div>';

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($input);
libxml_use_internal_errors(false);

$xpath = new DOMXpath($dom);
foreach($xpath->evaluate('//div[contains(@class, "clsA")]') as $clsA) {
    $clsA->setAttribute('class',$clsA->getAttribute('class').' myClass');
}
foreach($xpath->evaluate('//div[contains(@class, "clsB")]') as $clsB) {
    $clsB->setAttribute('class',$clsB->getAttribute('class').' clsInner');
}

echo $dom->saveXML($dom);
?>

Assuming that I have understood correctly and you wish to modify the DOM after the content has been generated somehow but before the client gets to see it the you can use output buffering , DOMDocument and DOMXPath to fudge around with the content however you see fit before the content is finally rendered. 假设我已正确理解并且您希望在以某种方式生成内容之后修改DOM,但是在客户端看到它之前,您可以使用output bufferingDOMDocumentDOMXPathDOMXPath处理内容,但是您认为适合内容最终呈现。 To view the result of the below code you need to look at the source code of the page when you run it ( if you run it "as-is" ) 要查看以下代码的结果,您需要在运行页面时查看页面的源代码(如果按原样运行)

<?php

    $outerClassName='myClass';
    $innerClassName='clsInner';

    $attrib='class';

    ob_start();
?>

<html>
    <head>
        <title>Manipulating the output</title>
    </head>
    <body>
        <div class="clsA">
          <div>
            <div class="clsB">
              Content (can be anything including clsA, clsB etc.)
            </div>
          </div>
        </div>

        <div class="clsC">
          <div class="clsB">
              Content (can be anything including clsC, clsB etc.)
          </div>
        </div>
    </body>
</html>
<?php
    $dom=new DOMDocument;
    $dom->loadHTML( ob_get_contents() );
    $xp=new DOMXPath( $dom );
    $col = $xp->query( '//div' );



    if( $col->length > 0 ){
        foreach( $col as $node ){

            if( !stristr( $node->getAttribute( $attrib ), $outerClassName ) ) {
                $children=$xp->query( 'div', $node );

                if( $children->length > 0 ){
                    if( $node->hasAttribute( $attrib ) ) $node->setAttribute( $attrib, trim( $node->getAttribute( $attrib ) . ' ' . $outerClassName ) );
                } else {
                    if( $node->hasAttribute( $attrib ) ) $node->setAttribute( $attrib, trim( $node->getAttribute( $attrib ) . ' ' . $innerClassName ) );
                }
            }
        }
        ob_clean();
        echo $dom->saveHTML();
    }


    $dom = $xp = $col = $children = null;
    ob_end_flush();
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM