Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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文档字符串_Php_Domdocument - Fatal编程技术网

操作PHP文档字符串

操作PHP文档字符串,php,domdocument,Php,Domdocument,我想删除domdocument html中的元素标记 我有点像 this is the <a href='#'>test link</a> here and <a href='#'>there</a>. 我的代码 $dom = new DomDocument(); $dom->loadHTML($html); $atags=$dom->getElementsByTagName('a'); foreach($atags as

我想删除domdocument html中的元素标记

我有点像

this is the <a href='#'>test link</a> here and <a href='#'>there</a>.
我的代码

 $dom = new DomDocument();
 $dom->loadHTML($html);
 $atags=$dom->getElementsByTagName('a');

 foreach($atags as $atag){
     $value = $atag->nodeValue;
//I can get the test link and there value but I don't know how to remove the a tag.                              
     }

谢谢你的帮助

您正在寻找一个名为的方法

要利用这一点,您需要为
$value
()创建一个
DOMText
,还需要
getElementsByTagName
返回一个自更新列表,因此当您替换第一个元素,然后转到第二个元素时,已经没有第二个元素了,只剩下一个a元素

相反,您需要一段时间来处理第一项:

$atags = $dom->getElementsByTagName('a');
while ($atag = $atags->item(0))
{
    $node = $dom->createTextNode($atag->nodeValue);
    $atag->parentNode->replaceChild($node, $atag);
}

沿着这些思路的东西应该可以做到。

你可以使用
strip\u标签
——它应该按照你的要求去做

<?php

$string = "this is the <a href='#'>test link</a> here and <a href='#'>there</a>.";

echo strip_tags($string);

// output: this is the test link here and there.

您正在寻找一个名为的方法。我已经尝试过了,但不知道为什么在您的标记工作时,只有第一个
标记会被替换。您正在使用的while循环类型成功了!nice@hek2mgl:是的,这是我写的一条注释:getElementsByTagName的列表会自动更新到所有使用该标记名的当前元素。如果删除其中的一个元素,列表将更改。
<?php

$string = "this is the <a href='#'>test link</a> here and <a href='#'>there</a>.";

echo strip_tags($string);

// output: this is the test link here and there.