PHP Conference Nagoya 2025

DOMElement::insertAdjacentText

(PHP 8 >= 8.3.0)

DOMElement::insertAdjacentTextInsert adjacent text

说明

public DOMElement::insertAdjacentText(string $where, string $data): void

Inserts text at a relative position given by where.

参数

where

  • beforebegin - Insert before the target element.
  • afterbegin - Insert as the first child of the target element.
  • beforeend - Insert as the last child of the target element.
  • afterend - Insert after the target element.

data

The string to insert.

返回值

没有返回值。

示例

示例 #1 DOMElement::insertAdjacentText() example

<?php

$dom
= new DOMDocument();
$dom->loadXML('<?xml version="1.0"?><container><p>H</p></container>');

$container = $dom->documentElement;
$p = $container->firstElementChild;

$p->insertAdjacentText("afterbegin", "P");
$p->insertAdjacentText("beforeend", "P");

echo
$dom->saveXML();
?>

以上示例会输出:

<?xml version="1.0"?>
<container><p>PHP</p></container>

参见

添加备注

用户贡献的备注

此页面尚无用户贡献的备注。
To Top