【问题标题】:How to strip html tag for elements that haw no ID and Class如何去除没有 ID 和 Class 的元素的 html 标签
【发布时间】:2020-05-25 16:19:19
【问题描述】:

长话短说!我需要删除所有没有设置 ID 和 Class 的 SPAN 标签。我发现 DOMXPath 查询选择器有效,但我不知道如何删除标签并保持文本不变。

注意第一个foreach:第一个foreach是循环语句,所以我需要将所有处理后的值取回$splited_sentences数组。

foreach ($splited_sentences as $sentence_key => $sentence_value) {
  $html_dom = Html::load($sentence_value);
    $xpath = new DOMXPath($html_dom);

    foreach ($xpath->query('//span[not(@id) and not(@class)]') as $element_key => $element) {
      // Not working Idea, all commented code is just idea to represent needed outcome:
      // $just_text = stip_html($element);
      // $splited_sentences[$sentence_key] = str_replace($element, $just_text);
    }
}

【问题讨论】:

    标签: php dom domxpath


    【解决方案1】:

    您可以使用 strip_tags() 删除任何 html 标签尝试以下代码:

    foreach ($splited_sentences as $sentence_key => $sentence_value) {
      $html_dom = Html::load($sentence_value);
        $xpath = new DOMXPath($html_dom);
    
        foreach ($xpath->query('//span[not(@id) and not(@class)]') as $element_key => $element) {
            $just_text = strip_tags ($element);
            $splited_sentences[$sentence_key] = str_replace($element, $just_text);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-19
      • 1970-01-01
      • 2012-04-06
      相关资源
      最近更新 更多