Reputation: 5550
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?
Upvotes: 0
Views: 122
Reputation: 57121
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,
];
}
Upvotes: 1