html删除子元素无效,删除父元素,使用saveHTML保留DOMDocument中的所有内部子元素...

原始答案 h2>

这个解决方案相当冗长,但这是因为它通过扩展DOM来实现它,以使您的结束代码尽可能短。

sliceOutNode是魔术发生的地方。如果您有任何疑问,请与我们联系:

class DOMDocumentExtended extends DOMDocument

{

public function __construct( $version = "1.0", $encoding = "UTF-8" )

{

parent::__construct( $version, $encoding );

$this->registerNodeClass( "DOMElement", "DOMElementExtended" );

}

// This method will need to be removed once PHP supports LIBXML_NOXMLDECL

public function saveXML( DOMNode $node = NULL, $options = 0 )

{

$xml = parent::saveXML( $node, $options );

if( $options & LIBXML_NOXMLDECL )

{

$xml = $this->stripXMLDeclaration( $xml );

}

return $xml;

}

public function stripXMLDeclaration( $xml )

{

return preg_replace( "|[\n\r]?|i", "", $xml );

}

}

class DOMElementExtended extends DOMElement

{

public function sliceOutNode()

{

$nodeList = new DOMNodeListExtended( $this->childNodes );

$this->replaceNodeWithNode( $nodeList->toFragment( $this->ownerDocument ) );

}

public function replaceNodeWithNode( DOMNode $node )

{

return $this->parentNode->replaceChild( $node, $this );

}

}

class DOMNodeListExtended extends ArrayObject

{

public function __construct( $mixedNodeList )

{

parent::__construct( array() );

$this->setNodeList( $mixedNodeList );

}

private function setNodeList( $mixedNodeList )

{

if( $mixedNodeList instanceof DOMNodeList )

{

$this->exchangeArray( array() );

foreach( $mixedNodeList as $node )

{

$this->append( $node );

}

}

elseif( is_array( $mixedNodeList ) )

{

$this->exchangeArray( $mixedNodeList );

}

else

{

throw new DOMException( "DOMNodeListExtended only supports a DOMNodeList or array as its constructor parameter." );

}

}

public function toFragment( DOMDocument $contextDocument )

{

$fragment = $contextDocument->createDocumentFragment();

foreach( $this as $node )

{

$fragment->appendChild( $contextDocument->importNode( $node, true ) );

}

return $fragment;

}

// Built-in methods of the original DOMNodeList

public function item( $index )

{

return $this->offsetGet( $index );

}

public function __get( $name )

{

switch( $name )

{

case "length":

return $this->count();

break;

}

return false;

}

}

// Load HTML/XML using our fancy DOMDocumentExtended class

$doc = new DOMDocumentExtended();

$doc->loadHTML('

Title...

...to be one of those crowning achievements...

');

// Remove doctype node

$doc->doctype->parentNode->removeChild( $doc->doctype );

// Slice out html node

$html = $doc->getElementsByTagName("html")->item(0);

$html->sliceOutNode();

// Slice out body node

$body = $doc->getElementsByTagName("body")->item(0);

$body->sliceOutNode();

// Pick your poison: XML or HTML output

echo htmlentities( $doc->saveXML( NULL, LIBXML_NOXMLDECL ) );

echo htmlentities( $doc->saveHTML() );

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值