doushishi6513 2017-07-11 18:26
浏览 71
已采纳

Php Simple Html - 如何将DOM转换为元素

I'm using PHP Simple HTML DOM Parser library in my project, but I can't figure out how to make a method working.

First I convert a string into a DOM object:

$html = str_get_html($rarr[$i]);

the $rarr variable is an array of html string elements. I want to remove their class and title attributes, so I use the following code:

$html = $html->removeAttribute('class');
$html = $html->removeAttribute('title');

but I get the following error:

Fatal error: Call to undefined method simple_html_dom::removeAttribute() in /scripts/defios.php on line 198

According to Documentation, the str_get_html() Creates a DOM object from a string. and I think the removeAttribute() method is not a DOM method but an Element method and that's why I get the error. So I need to convert somehow the DOM to Element. I think the find() method would do the job, but the problem is that I can't use it because the html elements in the array are randomly (some are divs, spans and they don't have a common class or id), so the method doesn't really help me. More the DOM itself is the element so I do not want to select something inside the DOM but to convert the entire DOM to an Element.

All I need to do is to remove that class and title, so any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • dongshang1768 2017-07-12 00:09
    关注

    Here's how I would remove those:

    foreach($html->find('[class]') as $el) $el->removeAttribute('class');
    foreach($html->find('[title]') as $el) $el->removeAttribute('title');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里