How can I change the pattern so that it matches p tags with attributes or without? Currently, it only matches on p tags without attributes.
$replaced_content = preg_replace_callback('#(<p>.*?</p>)#', 'p_callback', $content);
Example content #1:
<div style="margin: 10px 0;">
<a href="test.jpg" target="_blank" rel="nofollow">
<img style="float: left; margin: 10px; max-width: 25%;" title="test" src="test.jpg" alt="test" />
</a>
<p>
<a href="test" target="_blank" rel="nofollow">
<img title="PDF file" src="test.png" alt="PDF file" />
<span style="font-weight: bold; text-transform: capitalize;">Test ...</span>
</a>
<span>test <strong>test</strong> test? test, test, i.e., </span>
<a href="test.pdf" target="_blank" rel="nofollow"> ... Get Content Here</a>
</p>
</div>
Example content #2
<div style="margin: 10px 0;">
<a href="test.jpg" target="_blank" rel="nofollow">
<img style="float: left; margin: 10px; max-width: 25%;" title="test" src="test.jpg" alt="test" />
</a>
<p style="margin:10px; float:left">
<a href="test" target="_blank" rel="nofollow">
<img title="PDF file" src="test.png" alt="PDF file" />
<span style="font-weight: bold; text-transform: capitalize;">Test ...</span>
</a>
<span>test <strong>test</strong> test? test, test, i.e., </span>
<a href="test.pdf" target="_blank" rel="nofollow"> ... Get Content Here</a>
</p>
</div>
Use the s (PCRE_DOTALL) modifier:
Edited according to Dor Shemer’s comment