Php 如何通过DOMDocument::saveHTML在不改变字符串的情况下获取img标记?
我有这样的字符串:Php 如何通过DOMDocument::saveHTML在不改变字符串的情况下获取img标记?,php,domdocument,Php,Domdocument,我有这样的字符串: ...。すっきりと整理され...のです。<br /><br /><img src="/photo/images/2016/03/25/0_3316.jpg" alt="漢字漢字漢字" /><br /><br />「あのと... すっきりと整理され...のです。あのと... 我正试图通过DOMDocument::saveHTML获取img标记: $topic_conv = mb_convert_encoding(h
...。すっきりと整理され...のです。<br /><br /><img src="/photo/images/2016/03/25/0_3316.jpg" alt="漢字漢字漢字" /><br /><br />「あのと...
すっきりと整理され...のです。
あのと...
我正试图通过DOMDocument::saveHTML获取img标记:
$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'])));
}
$topic\u conv=mb\u convert\u编码(htmlspecialchars\u解码($topic['topic']['body']),'HTML-ENTITIES','UTF-8');
$doc->loadHTML($topic\u conv);
$img_tags=$doc->getElementsByTagName('img');
foreach($img_标记为$tag){
$img_alt=$tag->getAttribute('alt');
如果(空($img_alt))继续;
$img=$doc->saveHTML($tag);
var_dump($img);
str_replace($img,$img.”“$img_alt.”),htmlspecialchars_decode($topic['topic']['body']);
}
输出为:
它不同于原始字符串-没有空格和/或在结束括号之前,它应该有/>
,而不仅仅是
。
我的下一行代码stru replace不起作用,因为它不匹配
也许有人知道如何获得原始字符串中的img标记?如果您仍然需要它,请尝试以下操作:
str_replace($img, $img . "<span style='{$img_style_caption}'>" . $img_alt . " /" . "</span>", htmlspecialchars_decode($topic['Topic']['body'])));
str_replace($img,$img.”“$img_alt.”/”,htmlspecialchars_decode($topic['topic']['body']));
PS:代码来自我的头脑(因此未经测试)。如果有任何问题请告诉我…related@Ghost非常感谢您的链接。