douou6696 2016-11-20 14:44
浏览 48
已采纳

php DOMDocument提取与锚点或alt的链接

I which to extract all the link include on page with anchor or alt attribute on image include in the links if this one come first.

$html = '<a href="lien.fr">Anchor</a>';

Must return "lien.fr;Anchor"

$html = '<a href="lien.fr"><img alt="Alt Anchor">Anchor</a>';

Must return "lien.fr;Alt Anchor"

$html = '<a href="lien.fr">Anchor<img alt="Alt Anchor"></a>';

Must return "lien.fr;Anchor"

I did:

$doc = new DOMDocument();
$doc->loadHTML($html);

$out = "";
$n = 0;
$links = $doc->getElementsByTagName('a');

foreach ($links as $element) {
    $href = $img_alt = $anchor = "";
    $href = $element->getAttribute('href');
    $n++;
    if (!strrpos($href, "panier?")) {

        if ($element->firstChild->nodeName == "img") {

            $imgs = $element->getElementsByTagName('img');

            foreach ($imgs as $img) {
                if ($anchor = $img->getAttribute('alt')) {
                    break;
                }
            }
        }

        if (($anchor == "") && ($element->nodeValue)) {
            $anchor = $element->nodeValue;
        }

        $out[$n]['link'] = $href;
        $out[$n]['anchor'] = $anchor;
    }
}

This seems to work but if there some space or indentation it doesn't as

$html = '<a href="link.fr">
                    <img src="ceinture-gris" alt="alt anchor"/>
                </a>';

the $element->firstChild->nodeName will be text

  • 写回答

1条回答 默认 最新

  • dongluanjie8678 2016-11-20 15:13
    关注

    Something like this:

    $doc = new DOMDocument();
    $doc->loadHTML($html);
    
    // Output texts that will later be joined with ';'
    $out = [];
    // Maximum number of items to add to $out
    $max_out_items = 2;
    // List of img tag attributes that will be parsed by the loop below
    // (in the order specified in this array!)
    $img_attributes = ['alt', 'src', 'title'];
    
    $links = $doc->getElementsByTagName('a');
    foreach ($links as $element) {
      if ($href = trim($element->getAttribute('href'))) {
        $out []= $href;
        if (count($out) >= $max_out_items)
          break;
      }
    
      foreach ($element->childNodes as $child) {
        if ($child->nodeType === XML_TEXT_NODE &&
          $text = trim($child->nodeValue))
        {
          $out []= $text;
          if (count($out) >= $max_out_items)
            break;
        } elseif ($child->nodeName == 'img') {
          foreach ($img_attributes as $attr_name) {
            if ($attr_value = trim($child->getAttribute($attr_name))) {
              $out []= $attr_value;
              if (count($out) >= $max_out_items)
                goto Result;
            }
          }
        }
      }
    }
    
    Result:
    echo $out = implode(';', $out);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图