Normally you would do this with a stylesheet, but this is for a newsletter so have to use inline styles.
The newsletter contains several blocks wrapped in a div with the tmp class. The content will be placed inside this div, with possibly 1 image. This image is where I want to add an inline style to.
This code bellow works as long as there is no <p> in the same div. But once you add some text to it, it stops working. Obviously there will be text in a newsletter. I got this code form another form, i tried to play around with the code a bit to get it to work, but no success so far.
If you would remove the <p> from the $str, it does work fine. But I want to work with multiple <p> after the img. Any idea's on how to make this work with <p> after the image?
$str = '<div class="tmp">
<img width="140" height="140" align="left" src="/img/text/my-image.jpg">
$str = preg_replace('/\\s\\s+/', "", $str);
$pattern = "/<div class=\\"tmp\\">(\\\\w+)?<img ([^>]+)>(\\\\w+)?<\\/div>/is";
$replacement = "<div class=\\"tmp\\">\\\\1 <img style=\\"float:left; margin:0 10px 10px 0;\\" \\\\2 /> \\\\3</div>";
$str = preg_replace($pattern, $replacement, $str);