Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.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
如何更改HTML元素';将文本转换为PHP中的另一个元素?_Php_Html_Dom_Domdocument - Fatal编程技术网

如何更改HTML元素';将文本转换为PHP中的另一个元素?

如何更改HTML元素';将文本转换为PHP中的另一个元素?,php,html,dom,domdocument,Php,Html,Dom,Domdocument,正如标题所说,我想使用PHP将DOM元素的textContent更改为span。就像这样: <div class="dummy">Here is my content.</div> 但由于某种原因,没有一个有效。在这之后,我想用我想插入的新跨距将一个新的子元素附加到元素中 我怎样才能做到这一点? 提前谢谢 编辑:如果要设置如下元素的文本内容,可以设置nodeValue属性 $el = $dom->getElementById('foo'); $el->nod

正如标题所说,我想使用PHP将DOM元素的textContent更改为span。就像这样:

<div class="dummy">Here is my content.</div>
但由于某种原因,没有一个有效。在这之后,我想用我想插入的新跨距将一个新的子元素附加到元素中

我怎样才能做到这一点? 提前谢谢


编辑:

如果要设置如下元素的文本内容,可以设置nodeValue属性

$el = $dom->getElementById('foo');
$el->nodeValue = 'hello world';
这会自动转义<和>,因此不能像这样插入HTML

因此,要做到这一点,你可以:

DOMDocument::createDocumentFragment:

$frag = $dom->createDocumentFragment();
$frag->appendXML('<h1>foo</h1>');
$el->appendChild($frag);
DOMDocument::createDocumentFragment:
$frag=$dom->createDocumentFragment();
$frag->appendXML('foo');
$el->appendChild($frag);

如果要设置如下元素的文本内容,可以设置nodeValue属性

$el = $dom->getElementById('foo');
$el->nodeValue = 'hello world';
这会自动转义<和>,因此不能像这样插入HTML

因此,要做到这一点,你可以:

DOMDocument::createDocumentFragment:

$frag = $dom->createDocumentFragment();
$frag->appendXML('<h1>foo</h1>');
$el->appendChild($frag);
DOMDocument::createDocumentFragment:
$frag=$dom->createDocumentFragment();
$frag->appendXML('foo');
$el->appendChild($frag);
试试这个

$content='aaaaaaaaa';
$DOM_internal_HTML=new DOMDocument();
$DOM_internal_HTML->loadHTML(mb_convert_编码($content,'HTML-ENTITIES','UTF-8'));
$content\u node=$DOM\u internal\u HTML->getElementsByTagName('div')->项(0);
$content\u node->textContent='';
试试这个

$content='aaaaaaaaa';
$DOM_internal_HTML=new DOMDocument();
$DOM_internal_HTML->loadHTML(mb_convert_编码($content,'HTML-ENTITIES','UTF-8'));
$content\u node=$DOM\u internal\u HTML->getElementsByTagName('div')->项(0);
$content\u node->textContent='';

谢谢,我在另一个问题中看到了这个答案,但它不起作用。无法让它起作用。谢谢,我在另一个问题中看到了这个答案,但它不起作用。无法让它起作用。我尝试了这个,它起作用了。如果您没有看到标签是因为标签在browserI中呈现,我基本上也会这样做,但我使用$dom->loadHTMLFile($html);因为我必须加载一个文件,但它不工作。这可能是个问题吗?尝试使用文件获取内容并再次测试!在我的问题中增加了我的全部功能。你能看一看吗?我试了一下,效果很好。如果您没有看到标签是因为标签在browserI中呈现,我基本上也会这样做,但我使用$dom->loadHTMLFile($html);因为我必须加载一个文件,但它不工作。这可能是个问题吗?尝试使用文件获取内容并再次测试!在我的问题中增加了我的全部功能。你能看一下吗?