PHP Regex to get link and size...

Posted on 2009-04-23
Last Modified: 2013-12-13

I need to open a html page and read the file, then i need to extract two things from _many_ blocks that look like this:

<tr class="text">
<td><a href="">Download</a></td>
<td align="left"><span class="text">something here</span></td>
<td align="center">82 MB</td>

I *only* want the links that have the text "Download", note that there are plenty of other links on the page both with the text 'Download' and without. Lastly i need the filesize which will always be in the same _place_ as above.

So the above  should give me an array:
someArray ( [link1] => ""  [size1] =>  "82 MB")
Question by:lopband

    Accepted Solution

    If you are not looking for a strictly regex solution, check my attached code... its dirty, but it should work
    $handle = @fopen("inputfile.html", "r");
    if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle, 2000);
    		//$i++;echo $i.". ".$buffer;
    		$explode1 = explode("href=\"", $buffer);
    		$explode2 = explode("\">Download", $explode1[1]);
    		$the_urls[]= $explode2[0];
    				if(trim($buffer) !="")
    			else if($size_line_counter==2)
    				if(trim($buffer) !="")
    						$explode3 = explode(">", $buffer);
    						$explode4 = explode("<", $explode3[1]);
    echo $the_urls[$i]." ".$the_size[$i]."<br>";

    Open in new window

    LVL 1

    Author Comment

    That does work pretty well, but i was looking for a regex solution...

    if nobody else posts i'll give you the points.

    LVL 1

    Author Comment

    ok, it seems to be working exactly as planned so i'll give you the pts.. cheers!
    LVL 1

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now