【发布时间】:2012-01-22 19:21:15
【问题描述】:
是否有任何 PHP 框架/库可以让我执行以下操作:
$element = $html->getElementByName("name");
$element->changeAttribute("class", "myClass");
echo $html->display;
我知道使用 javascript 可以实现类似的功能,但我需要它是 PHP。
【问题讨论】:
标签: php html frameworks
是否有任何 PHP 框架/库可以让我执行以下操作:
$element = $html->getElementByName("name");
$element->changeAttribute("class", "myClass");
echo $html->display;
我知道使用 javascript 可以实现类似的功能,但我需要它是 PHP。
【问题讨论】:
标签: php html frameworks
不需要框架。只需使用PHP's 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)
【讨论】: