How to add <br> tag after <img> tag?
I want to add <br>
tag after <img>
tag, my code:
$string = "< div> < img src="http://example.com/a.jpg"> < /div>";
$pattern = '/< img*+开发者_JAVA技巧>+/i';
$replacement = '${1}'.'< br>';
echo preg_replace($pattern, $replacement, $string);
but it does not work.
Do you absolutely need the <br>
tag added?
You could do it using CSS instead:
img { display:block; margin-bottom:10px; }
If you only wanted to affect images within a certain section, you could limit it using:
.article img { display:block; margin-bottom:10px; }
This sounds like you are trying to use <br />
when some CSS would be better.
However, if you do intend to add a br
after every img
element, I would strongly suggest you skip messing around with the regex and use a DOM parser.
精彩评论