dt246813579 2019-06-17 18:20 采纳率: 0%
浏览 69
已采纳

如何使用PHP DOMDocument()检索子元素中的值?

I have a $body variable that I am retrieving from a post. The user may or may not post a picture.

When it posts a picture I must retrieve some information about the picture and also sometimes the user may write a caption for the picture.

This is the html without caption:

<figure class="image"><img src="/storage/5/articles/pictures/asdf87.jpeg"></figure>

And this is an example with caption:

<figure class="image"><img src="/storage/5/articles/pictures/asdf87.jpeg"><figcaption>test_caption</figcaption></figure>

This is the code I have so far:

$body = '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at dictum lectus. Ut volutpat pulvinar dui, quis elementum est bibendum sit amet. Curabitur a tempor augue. Nulla bibendum porttitor lacinia. Pellentesque tempor sem sed condimentum lobortis. Duis vulputate ante vel enim auctor luctus.</p><figure class="image"><img src="/storage/5/articles/pictures/1560793567749_d20caec3b48a1eef164cb4ca81ba2587.jpeg"><figcaption>tudo de ensaio</figcaption></figure><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at dictum lectus. Ut volutpat pulvinar dui, quis elementum est bibendum sit amet. Curabitur a tempor augue. Nulla bibendum porttitor lacinia. Pellentesque tempor sem sed condimentum lobortis. Duis vulputate ante vel enim auctor luctus.</p><figure class="image"><img src="/storage/5/articles/pictures/1560793584944_4c614360da93c0a041b22e537de151eb.jpeg"><figcaption>tb ensaio gota</figcaption></figure><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at dictum lectus. Ut volutpat pulvinar dui, quis elementum est bibendum sit amet. Curabitur a tempor augue. Nulla bibendum porttitor lacinia. Pellentesque tempor sem sed condimentum lobortis. Duis vulputate ante vel enim auctor luctus.</p><figure class="image"><img src="/storage/5/articles/pictures/1560793600192_21ae1a72068eff5f1c6e0238501b06a6.jpeg"><figcaption>tb ens colors</figcaption></figure><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at dictum lectus. Ut volutpat pulvinar dui, quis elementum est bibendum sit amet. Curabitur a tempor augue. Nulla bibendum porttitor lacinia. Pellentesque tempor sem sed condimentum lobortis. Duis vulputate ante vel enim auctor luctus.</p>' ;

        $dom_err = libxml_use_internal_errors(true);
        $dom = new \DOMDocument();
        $dom->loadHtml($body, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        $xpath = new \DOMXPath($dom);
        $imgs = [];
        foreach ($xpath->query("//figure/img") as $img) {
            $src = $img->getAttribute('src');
            if (preg_match('#/storage/(.*)/articles/pictures/(.*)#', $src, $result)) {
                $imgs[] = [
                    'id'      => $result[1],
                    'name'    => $result[2],
                    'caption' => $img->item(0)->textContent,
                ];
            }
        }
        libxml_clear_errors();
        libxml_use_internal_errors($dom_err);

I am trying to retrieve the caption in this portion of the code 'caption' => $img->item(0)->textContent, and it is not working.

What am I missing?

  • 写回答

1条回答 默认 最新

  • doubo3384 2019-06-17 18:45
    关注

    What you can do is to look at the next element from the <img> tag (using nextSibling) and if this is a <figcaption> element, then set the caption text to be the text content of it, otherwise set it to be blank...

    if (preg_match('#/storage/(.*)/articles/pictures/(.*)#', $src, $result)) {
        $caption = $img->nextSibling;
        if ( $caption->localName == "figcaption" )  {
            $captionText = $caption->textContent;
        }
        else    {
            $captionText = "";
        }
        $imgs[] = [
            'id'      => $result[1],
            'name'    => $result[2],
            'caption' => $captionText,
        ];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站