2011-12-17 27 views
1

是否有任何PHP框架/库,允许我做这样的事情:PHP框架,允许我处理HTML?

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

我知道,这样的事情是可能用JavaScript,但我需要它是PHP。

+0

你为什么要? – 2011-12-17 21:54:28

回答

1

例子:

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

2

查看DOMDocument课程中的各种方法。当然,请记住,在你操纵它之前,你需要使用loadHTML将你的html加载到DOMDocument对象中。