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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器