Retrieving info using regex problem

Discussion in 'PHP' started by papa_face, Dec 5, 2010.

  1. #1
    Hi

    I have the following HTML:
    																	<div class="categories">
    				<a href="http://www.domain.com/asp-tutorials" class="catTitle">
    				<img src="http://www.domain.com/img/29.jpg" alt="ASP Tutorials"></a>
    				<a href="http://www.domain.com/asp-tutorials" class="floatLeft catTitle">ASP Tutorials</a><a href="http://www.domain.com/asp-tutorials/rss/categories.php?chid=29" title="RSS Feed for all ASP Tutorials!" class="rss"><img src="http://www.domain.com/images/rss_icon.gif" width="28" height="12" alt=""/></a>
    				</div>
    Code (markup):
    I am trying to retrieve the href in this line using regex:
    <a href="http://www.domain.com/asp-tutorials" class="catTitle">
    Code (markup):
    I have this:
    $expr = '/<div class="categories">.*?<a href="(.*?)" class="catTitle">.*?<img src=".*?" alt=".*?"><\/a>.*?<\/div>/s';
    preg_match_all($expr,$file,$info,PREG_SET_ORDER);
    
    print_r($info);
    Code (markup):
    But it is returning no results.
    There are a few categories on the page which I'm trying to retrieve the links for.
    Anyone have any idea why?
     
    papa_face, Dec 5, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    Try this in a test file.php
    
    $page = '<div class="categories">
    				<a href="http://www.domain.com/asp-tutorials" class="catTitle">
    				<img src="http://www.domain.com/img/29.jpg" alt="ASP Tutorials"></a>
    				<a href="http://www.domain.com/asp-tutorials" class="floatLeft catTitle">ASP Tutorials</a><a href="http://www.domain.com/asp-tutorials/rss/categories.php?chid=29" title="RSS Feed for all ASP Tutorials!" class="rss"><img src="http://www.domain.com/images/rss_icon.gif" width="28" height="12" alt=""/></a>
    				</div>';
    
    preg_match_all('#href="([^"]+)"#i',$page, $href);
    print_r($href);
    
    
    
    echo "<br /><br />";
    echo $href[1][0];
    echo "<br /><br />";
    echo $href[1][1];
    echo "<br /><br />";
    echo $href[1][2];
    echo "<br /><br />";
    echo $href[1][3];
    echo "<br /><br />";
    echo $href[1][4];
    
    PHP:
     
    MyVodaFone, Dec 5, 2010 IP
  3. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #3
    There are lots of div blocks on the same page with the same layout but contain info I don't want. It needs to specifically home in on "class="catTitle">"
     
    papa_face, Dec 5, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    Theres only 1 match for that ?

    
    preg_match_all('#href="([^"]*)" class="catTitle"#i',$page, $href);
    
    PHP:
    Do you need something different ?
     
    MyVodaFone, Dec 5, 2010 IP