是否有任何PHP框架/库允许我做这样的事情:
$element = $html->getElementByName("name");
$element->changeAttribute("class", "myClass");
echo $html->display;
我知道这样的东西是可能与javascript,但我需要它是PHP。
不需要框架。只需使用PHP的DOMDocument
查看DOMDocument类中的各种方法。当然,请记住,在操作它之前,您需要使用loadHTML将html加载到DOMDocument对象中。
示例:
<?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使用loadHTMLFile
或loadHTML
而不是load
)