Как получить определённую ссылку через preg_match_all?
правильно - использовать dom-парсер, а не регулярки<?php

$html = <<<'HTML'
<div class="ds51" id="idds51">
<p class="ds52" id="idds52">
<a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/000.jpg" alt="Комикс Веном том 4" title="Комикс Веном том 4"><img src="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002.jpg" rel="nofollow" alt="" />#002</a></p>
<div class="hidden">
<a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/001.jpg" title="Стр. 1"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/002.jpg" title="Стр. 2"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/003.jpg" title="Стр. 3"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/004.jpg" title="Стр. 4"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/005.jpg" title="Стр. 5"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/006.jpg" title="Стр. 6"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/007.jpg" title="Стр. 7"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/008.jpg" title="Стр. 8"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/009.jpg" title="Стр. 9"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/010.jpg" title="Стр. 10"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/011.jpg" title="Стр. 11"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/012.jpg" title="Стр. 12"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/013.jpg" title="Стр. 13"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/014.jpg" title="Стр. 14"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/015.jpg" title="Стр. 15"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/016.jpg" title="Стр. 16"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/017.jpg" title="Стр. 17"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/018.jpg" title="Стр. 18"></a><br /><a class="fancybox" rel="gallery002, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/019.jpg" title="Стр. 19"></a>
</div>

</div>
<div class="ds51" id="idds51">
<p class="ds52" id="idds52">
<a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/000.jpg" alt="Комикс Веном том 4" title="Комикс Веном том 4"><img src="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003.jpg" rel="nofollow" alt="" />#003</a></p>
<div class="hidden">
<a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/001.jpg" title="Стр. 1"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/002.jpg" title="Стр. 2"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/003.jpg" title="Стр. 3"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/004.jpg" title="Стр. 4"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/005.jpg" title="Стр. 5"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/006.jpg" title="Стр. 6"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/007.jpg" title="Стр. 7"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/008.jpg" title="Стр. 8"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/009.jpg" title="Стр. 9"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/010.jpg" title="Стр. 10"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/011.jpg" title="Стр. 11"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/012.jpg" title="Стр. 12"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/013.jpg" title="Стр. 13"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/014.jpg" title="Стр. 14"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/015.jpg" title="Стр. 15"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/016.jpg" title="Стр. 16"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/017.jpg" title="Стр. 17"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/018.jpg" title="Стр. 18"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/019.jpg" title="Стр. 19"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/020.jpg" title="Стр. 20"></a><br /><a class="fancybox" rel="gallery003, nofollow" href="http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/021.jpg" title="Стр. 21"></a>
</div>
HTML;

$dom = new DOMDocument;
@$dom->loadHTML($html);

$galleryNumber = '002';
$xpath = new DomXPath($dom);
$links = $xpath->query('//a[@rel="gallery' . $galleryNumber . ', nofollow"]');
echo $links->item($links->length - 1)->getAttribute('href'), PHP_EOL;

$galleryNumber = '003';
$xpath = new DomXPath($dom);
$links = $xpath->query('//a[@rel="gallery' . $galleryNumber . ', nofollow"]');
echo $links->item($links->length - 1)->getAttribute('href'), PHP_EOL;

// http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-002/019.jpg
// http://img.drawnstories.ru/img/Marvel-Comics/Spider-Man/Venom/Venom-v4/Venom-v4-003/021.jpg
preg_match_all('.*rel="gallery002,.*href="(http:\/\/.*?)"', $text0, $out1);
Соответственно, с параметром:$name = 'gallery002';
preg_match_all('.*rel="'.$name.',.*href="(http:\/\/.*?)"', $text0, $out1);