Simple Regular Expression Help

Discussion in 'PHP' started by ColorWP.com, Jun 28, 2010.

  1. #1
    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:

     
    ColorWP.com, Jun 28, 2010 IP
  2. Kaimi

    Kaimi Peon

    Messages:
    60
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?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:
     
    Kaimi, Jun 28, 2010 IP
    www.Andro.ws likes this.
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3
    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.
     
    MyVodaFone, Jun 28, 2010 IP