i have this html content : 

```
<p><img src="##" />
</p>
<p>

<img src="##" />
</p>
<p>
</p>
<p class="ss"><a href="ss">
    <img src="####" />
</a></p>

```

i want to extract all paragraphs and all their images and reformat the html so i can have images first then the folowing paragraph this is an example : 

```
<img src="##" /><p>
</p>
<img src="##" />
<p>

</p>
<p>
</p>
<img src="####" />
<p class="ss"><a href="ss">

</a></p>

```

i tried something but it won't work : 

```
$result = preg_replace('/(<p\b[^><]*)>(.+?)(<img([^>]*)\/>)(.+?)(<\/p>)/is', '$2 $4', $text);

```