1

Possible Duplicate:
How to parse HTML with PHP?
Grabbing the href attribute of an A element

I have some random text with images in a href tag like this:

<a title="Some title" rel="lightbox" href="http://www.test.com/DSCF0733.jpg"><img class="alignleft size-thumbnail wp-image-504" title="some title" src="http://www.test.com/Dhghjkhjl33-150x150.jpg" alt="description" width="145" height="145" /></a>

I want to find them all and put to an array. Text can contain other links, but we need only with rel lightbox. Please, help!

Community
  • 1
  • 1
Zhlobopotam
  • 173
  • 13

2 Answers2

2

You could use the built in DOMDocument(), simple yet effective & safer then regex...

<?php 
$site=file_get_contents('http://example.com');

$xml = new DOMDocument();
@$xml->loadHTML($site);


foreach($xml->getElementsByTagName('a') as $links) {
    //Check for lightbox within the link
    if($links->getAttribute('rel')=='lightbox'){ 
        //Assign
        $imgLinks[]=$links->getAttribute('href');
    }
}

print_r($imgLinks);
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • I think the OP want to find all `src` attributes of `img` tags that are inside `a` tags that have `rel="lightbox"`. You're (trying) to find all `href` values of `a` tags that have `rel="lightbox"`. – Bart Kiers Sep 07 '11 at 14:48
  • Also note that you spelled `'herf'` instead of `'href'`. – Bart Kiers Sep 07 '11 at 14:49
  • lol whoops on the typo ;s the op specifically said `href tag` and then `find them all and put to an array` thanks tho – Lawrence Cherone Sep 07 '11 at 16:57
1

For simplicity use phpQuery or QueryPath:

include "qp.phar";
foreach (htmlqp($html)->find("a[rel=lightbox]") as $a) {
    $links[] = $a->attr("href");
}

But you can also modify the contained text or other attributes. (The preg_replace part of your question might need elaboration.)

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291