Hello. I have this block of text inside a string: How do I match and get all the listed directors in this string with regular expression so that I have the following array as result: $array = array( "/name/nm0905152/"=>"Andy Wachowski", "/name/nm0905154/"=>"Lana Wachowski", ); PHP: Essentially, the href attribute's contents will be array key and the link text will be array value. Note that the A tag sometimes has other attributes, which the regular expression should ignore, like onclick="" or title="" or target="". Currently I use this, but I've got to nowhere: $director = preg_match_all('%^<a(?:.*)>(.*)</a>$%i',$original,$new); PHP:
<?php $str = '<div id="director-info" class="info"> <h5>Directors:</h5> <div class="info-content"> <a href="/name/nm0905152/">Andy Wachowski</a><br/> <a href="/name/nm0905154/">Lana Wachowski</a><br/> </div> </div>'; preg_match_all('#href="(.+?)".*?>(.+?)</a>#i', $str, $match); $match = array_combine($match[1], $match[2]); print_r($match); ?> PHP:
I currently use this, retrieves 1 director function get_match($regex,$content) { preg_match($regex,$content,$matches); return $matches[1]; } PHP: $director = strip_tags(get_match('/<h5[^>]*>Director:<\/h5>(.*)<\/div>/isU',$imdb)); PHP: Might be of use, also if your interested your more then welcome to a copy of my script you just enter an imdb url and what you see on the site is what you get.