相关疑难解决方法(0)

如何使用DOMDocument类删除HTML元素

有没有办法通过使用DOMDocument类删除HTML元素?

php domdocument

11
推荐指数
2
解决办法
2万
查看次数

删除脚本和样式标记中的所有内容

我有一个名为变量的变量$articleText,它包含html代码.有scriptstyle内码<script><style>HTML元素.我想扫描$articleText并删除这些代码.如果我还可以删除实际的HTML元素<script>,</script>,<style></style>,我会做到这一点.

我想我需要使用正则表达式,但我不熟练.

有人可以帮忙吗?

我希望我能提供一些代码,但就像我说我不熟练的正则表达式,所以我没有任何东西可以显示.

我不能使用DOM.我需要专门针对这些特定标签使用正则表达式

html javascript php regex jquery

9
推荐指数
2
解决办法
2万
查看次数

PHP:无法从DomDocument中删除节点

我无法从DomDocument中删除节点(获取异常):

我的代码:

<?php
    function filterElements($htmlString) {
        $doc = new DOMDocument();
        $doc->loadHTML($htmlString);
        $nodes = $doc->getElementsByTagName('a');
        for ($i = 0; $i < $nodes->length; $i++) {
          $node=$nodes->item($i)
          if ($value->nodeValue == 'my_link') {
           $doc->removeChild($node);
          }
        }
    }
    $htmlString = '<div>begin..</div>this tool<a name="my_link">Beo</a> great!<div>.end</div>';
    filterKeyLinksElements($htmlString);
    ?>
Run Code Online (Sandbox Code Playgroud)

谢谢你,约瑟夫

php domdocument

2
推荐指数
1
解决办法
5448
查看次数

如何使用PHP DOMDocument删除属性?

使用这段XML:

<my_xml>
  <entities>
    <image url="lalala.com/img.jpg" id="img1" />
    <image url="trololo.com/img.jpg" id="img2" />
  </entities>
</my_xml> 
Run Code Online (Sandbox Code Playgroud)

我必须摆脱图像标签内的所有属性.所以,我做到了这一点:

<?php

$article = <<<XML
<my_xml>
  <entities>
    <image url="lalala.com/img.jpg" id="img1" />
    <image url="trololo.com/img.jpg" id="img2" />
  </entities>
</my_xml>  
XML;

$doc = new DOMDocument();
$doc->loadXML($article);
$dom_article = $doc->documentElement;
$entities = $dom_article->getElementsByTagName("entities");

foreach($entities->item(0)->childNodes as $child){ // get the image tags
  foreach($child->attributes as $att){ // get the attributes
    $child->removeAttributeNode($att); //remove the attribute
  }
}

?>
Run Code Online (Sandbox Code Playgroud)

不知何故,当我尝试删除foreach块中的from属性时,它看起来像内部指针丢失,并且它不会删除这两个属性.

还有另一种方法吗?

提前致谢.

php domdocument

2
推荐指数
1
解决办法
7757
查看次数

标签 统计

php ×4

domdocument ×3

html ×1

javascript ×1

jquery ×1

regex ×1