Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Libxml中的Wrap标记_Php_Libxml2 - Fatal编程技术网

Php Libxml中的Wrap标记

Php Libxml中的Wrap标记,php,libxml2,Php,Libxml2,我目前是在现有libxml代码的基础上构建的,找不到详细的文档 是否可以将标记环绕在节点上 我认为这会奏效: $tags = $doc->getElementsByTagName( 'pre' ); foreach( $tags as $tag ): $handler = $doc->createElement( 'div' ); $handler->setAttribute( 'class', 'pre_wrapper' ); $newnode =

我目前是在现有libxml代码的基础上构建的,找不到详细的文档

是否可以将标记环绕在节点上

我认为这会奏效:

$tags = $doc->getElementsByTagName( 'pre' );

foreach( $tags as $tag ):

    $handler = $doc->createElement( 'div' );
    $handler->setAttribute( 'class', 'pre_wrapper' );
    $newnode = $handler->appendChild( $tag );

    $tag->replaceNode( $newnode );

endforeach;

问题是
$handler->appendChild($tag)
将元素
$tag
从其原始位置取消链接,因此以下
replaceNode
没有预期效果。这意味着您必须交换通话顺序。此外,我找不到
replaceNode
方法,但必须在父节点上调用
replaceChild

# Create wrapper element
$handler = $doc->createElement('div');
$handler->setAttribute('class', 'pre_wrapper');
# Replace wrapped element with wrapper
$tag->parentNode->replaceChild($handler, $tag);
# Move wrapped element into wrapper
$handler->appendChild($tag);

不错的:)谢谢。我摆弄了好一阵子,但还是弄不明白:/