I have string like:
...。すっきりと整理され...のです。<br /><br /><img src="/photo/images/2016/03/25/0_3316.jpg" alt="漢字漢字漢字" /><br /><br />「あのと...
And I'm trying to get img tag by DOMDocument::saveHTML:
$topic_conv = mb_convert_encoding(htmlspecialchars_decode($topic['Topic']['body']), 'HTML-ENTITIES', 'UTF-8');
$doc->loadHTML($topic_conv);
$img_tags = $doc->getElementsByTagName('img');
foreach ($img_tags as $tag) {
$img_alt = $tag->getAttribute('alt');
if (empty($img_alt)) continue;
$img = $doc->saveHTML($tag);
var_dump($img);
str_replace($img, $img . "<span style='{$img_style_caption}'>" . $img_alt . "</span>", htmlspecialchars_decode($topic['Topic']['body'])));
}
The output is: <img src="/photo/images/2016/03/25/0_3316.jpg" alt="漢字漢字漢字">
It's different from the original string - there is no SPACE and / before the closing bracket, it should have />
instead of just >
.
And my next line of code str_replace doesn't work, because it doesn't match.
Maybe, someone know how can I get img tag as it is in the original string?