【问题标题】:PHP Framework that allows me to deal with HTML?允许我处理 HTML 的 PHP 框架?
【发布时间】:2012-01-22 19:21:15
【问题描述】:

是否有任何 PHP 框架/库可以让我执行以下操作:

$element = $html->getElementByName("name");
$element->changeAttribute("class", "myClass");
echo $html->display;

我知道使用 javascript 可以实现类似的功能,但我需要它是 PHP。

【问题讨论】:

    标签: php html frameworks


    【解决方案1】:

    不需要框架。只需使用PHP's DOMDocument

    【讨论】:

      【解决方案2】:

      查看DOMDocument 类中的各种方法。当然,请记住,您需要使用 loadHTML 将 html 加载到 DOMDocument 对象中,然后才能对其进行操作。

      【讨论】:

        【解决方案3】:

        例子:

          <?php
              $doc = new DOMDocument();
              $doc->load( 'books.xml' );
        
              $books = $doc->getElementsByTagName( "book" );
              foreach( $books as $book )
              {
                  $authors = $book->getElementsByTagName( "author" );
                  $author = $authors->item(0)->nodeValue;
        
                  $publishers = $book->getElementsByTagName( "publisher" );
                  $publisher = $publishers->item(0)->nodeValue;
        
                  $titles = $book->getElementsByTagName( "title" );
                  $title = $titles->item(0)->nodeValue;
        
                  echo "$title - $author - $publisher\n";
              }
          ?>
        

        只需使用 DOMDocument 类(对于 HTML,使用 loadHTMLFileloadHTML 而不是 load

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-10-05
          • 2015-03-19
          • 1970-01-01
          • 2020-05-20
          • 2011-07-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多