0
私は、文字列のように持っている:imgタグをDOMDocument :: saveHTMLによって文字列から変更せずに取得するには?
...。すっきりと整理され...のです。<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'])));
}
出力は次のようになります。<img src="/photo/images/2016/03/25/0_3316.jpg" alt="漢字漢字漢字">
それはオリジナルと違います文字列 - スペースがなく、閉じ括弧の前には、>
の代わりに/>
が必要です。 そして次のコード行str_replaceは一致しないので、機能しません。
元の文字列にあるようにimgタグを取得するにはどうすればよいでしょうか?
関連http://stackoverflow.com/questions/3145237/why-doesnt-php-dom-include-slash-on-self-closing-tags – Ghost
@Ghostありがとうリンクのための多く。 – whitesiroi