$re = '/(<a\b[^<]*?|(?<!^)\G)([^\/]*?)%20(?=(?![^\/]*\.jpg">)[^\/"]*">)/';
$str = '<a href="http://example.com/path/to-some-folder/another%20folder/one%20more520folder/34%20-%20kv34%20-%20tomba%20di%20thumtmose%20iii"> KV34 Tomba di Thutmose III</a>:</b> Nella foto, la pianta della tomba</font></p> </td> </tr> </tbody></table> </div> <div align="center"> <div align="center"> <table border="1" width="96%" id="table4"> <tbody align="left"><tr> <td width="11%" align="center"> <a href="http://example.com/path/to-some-folder/another%20folder/one%20more520folder/uploads/2012/02/some%20folder/another%20folder/06%20antichi%20egizi%20-%20Tomba%20di%20Amenofi.jpg"> <img border="0" src="http://example.com/path/to-some-folder/another%20folder/one%20more520folder/uploads/2012/02/some%20folder/another%20folder/06%20antichi%20egizi%20-%20Tomba%20di%20Amenofi%20ante.jpg" width="80" height="64" alt="/06 antichi egizi - Tomba di Amenofi" /></a></td> <td width="87%" class="style4"> <p class="style3"> <b><a href="http://example.com/path/to-some-folder/another%20folder/one%20more520folder/35%20KV35%20Tomba%20di%20Amenofi%20II">KV35 Tomba di Amenofi II</a>; </b> Nella foto, il re davanti alla dea Hathor che gli offra il segno della vita.</p> </td> </tr> <tr> <td width="11%" align="center"> <a href="http://example.com/path/to-some-folder/another%20folder/one%20more520folder/uploads/2012/02/some%20folder/another%20folder/24%20antichi%20egizi%20-%20tomba%20di%20Horemhab.jpg"> <img border="0" src="http://example.com/path/to-some-folder/another%20folder/one%20more520folder/uploads/2012/02/some%20folder/another%20folder/24%20antichi%20egizi%20-%20tomba%20di%20Horemhab%20ante.jpg" width="80" height="59" alt="24 antichi egizi - tomba di Horemhab" /></a></td> <td width="87%" class="style4"> <div class="style3"> <b><a href="http://example.com/path/to-some-folder/another%20folder/one%20more520folder/uploads/2012/02/some%20folder/another%20folder/57%20kv57%20tomba%20di%20horemhab"> KV 57Tomba di Horemhab</a><font face="Arial" size="2">: </font></b><font face="Arial" size="2">Nella foto, il faraone è raffigurato di fronte a varie divinità</font></div> </td> </tr> <tr> <td width="11%" align="center"> <a href="http://example.com/path/to-some-folder/another%20folder/one%20more520folder/uploads/2012/02/some%20folder/another%20folder/09%20antichi%20egizi%20-%20Tomba%20di%20Tutankhamen.jpg"> <img border="0" src="http://example.com/path/to-some-folder/another%20folder/one%20more520folder/uploads/2012/02/some%20folder/another%20folder/09%20antichi%20egizi%20-%20Tomba%20di%20Tutankhamen%20ante.jpg" width="80" height="92" alt="09 antichi egizi - Tomba di Tutankhamen" /></a></td> <td width="87%" class="style4"> <b><a href="http://example.com/path/to-some-folder/another%20folder/one%20more520folder/uploads/2012/02/some%20folder/another%20folder/62%20kv62%20tomba%20di%20tutankhamen">KV62 Tomba di Tutankhamen:</a></b> Nella foto, <font face="Arial" size="2">un particolare della decorazione, con la barca solare ed i babbuini che alludono alla prima ora del libro dell\'Amduat.</font></td> </tr> <tr> <td';
$subst = "\1\2-";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php