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

时间:2016-11-20 14:44:07

标签: php domdocument

我提取页面上包含锚点或alt属性的所有链接包含在链接中,如果这个链接首先出现。

func configureCell(iteminfo:ItemInfo , img1 : UIImage? , img2 : UIImage? ){

        if img1 != nil {
            imageViewItemPic.image = img1
        }
        else{
            print("hi1")
           imageViewItemPic.image = UIImage(named: "resto-placeholder.png")

            imageViewItemPic.setImageFromURL(url: iteminfo.imageUrl!)
        }


        if img2 != nil {
            imageViewCookerProfilePic.image = img2

        }
        else{
           imageViewItemPic.image = UIImage(named: "resto-placeholder.png")
            imageViewCookerProfilePic.setImageFromURL(url: iteminfo.cookerProfilePicUrl!)
        }

        labelItemHeading.text = iteminfo.heading
        labelItemDescription.text = iteminfo.description


    }

必须返回“lien.fr; Anchor”

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

必须返回“lien.fr; Alt Anchor”

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

必须返回“lien.fr; Anchor”

我做了:

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

这似乎有效但如果有一些空间或缩进则不行 如

$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;
    }
}

$ element-&gt; firstChild-&gt; nodeName将为text

1 个答案:

答案 0 :(得分:0)

这样的事情:

ruamel.yaml