Wrap div around hyperlink (a tag)
<p>I have a text with multiple links in it. One single link looks like</p>

<pre><code>&lt;a href="http://www.mydomain.com/files/test1.pdf" target="_blank"&gt;My link&lt;/a&gt;
</code></pre>

<p>First, I would like to wrap <code>div</code> tag around all <code>href</code> tags but only if <code>href</code> is beggining with <code>http://www.mydomain.com/files/</code>. So the link above (because it starts with that prefix) should become</p>

<pre><code>&lt;div class="myLink"&gt;
   &lt;a href="http://www.mydomain.com/files/test1.pdf" target="_blank"&gt;My link&lt;/a&gt;
&lt;/div&gt;
</code></pre>

<p>Then depending on <strong>file extension</strong> (ending characters of the <code>href</code>) add an image before a link so it becomes</p>

<pre><code>&lt;div class="myLink"&gt;
   &lt;img src="pdf_file_icon.png" alt="File type icon" /&gt;
   &lt;a href="http://www.mydomain.com/files/test1.pdf" target="_blank"&gt;My link&lt;/a&gt;
&lt;/div&gt;
</code></pre>

<p>And optionally add some my text after the link so the final version becomes</p>

<pre><code>&lt;div class="myLink"&gt;
   &lt;img src="pdf_file_icon.png" alt="File type icon" /&gt;
   &lt;a href="http://www.mydomain.com/files/test1.pdf" target="_blank"&gt;My link&lt;/a&gt;
   &lt;span class="fileSize"&gt;1.3 Mb&lt;/span&gt;
&lt;/div&gt;
</code></pre>

<p>I need solution for <strong>both PHP</strong> and <strong>JavaScript/jQuery</strong> <em>(I think it's easier for jQuery because of it's selector capabilites?)</em>.</p>

<hr>

<p>What I want to get is to move from regular <a href="http://www.google.com" rel="nofollow">text link</a> to something like this (don't bother with file size, it's pre-calculated so it's just like any other regular/dummy text)</p>

<p><img src="http://i.stack.imgur.com/my2XY.jpg" alt="enter image description here"></p>

<h2>Edit #1</h2>

<p>Here is my try in <strong>PHP</strong></p>

<pre><code>$regexp = "&lt;a\s[^&gt;]*href=(\"??)(http:\/\/www\.mydomain\.com\/files\/[^\" &gt;]*?)\\1[^&gt;]*&gt;(.*)&lt;\/a&gt;";
$text = preg_replace("/$regexp/siU", "&lt;div class=\"fileAttachment\"&gt;&lt;img src=\"pdf_file_icon.png\" alt=\"File type icon\" /&gt;&lt;a href=\"$2\" target=\"_blank\"&gt;$3&lt;/a&gt;&lt;/div&gt;", $text);
</code></pre>

<p>But I'm not sure how immediately to check for file extension and depending on it insert appropriate image (pdf_file_icon.png)?</p>
