Re: getting what's between <td></td>
| From: | Simon Garner | Date: | Thu, 22 Feb 2001 09:09:00 +0000 |
| Subject: | Re: getting what's between <td></td> | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-41109@lists.php.net to get a copy of this message | ||
From: "Tyler Longren" <tyler.longren@midiowa.net>
> Hello,
>
> I've been reading a LOT on how to solve my problem today, but haven't been
> able to come up with anything yet.
>
> Here's my problem:
> I have an html file that contains many table rows like this:
>
> <tr><td>Item1</td><td>Item1 Again</td></tr>
> <tr><td>Item2</td><td>Item2 Again</td></tr>
> <tr><td>Item3</tr><td>Item3 Again</td></tr>
> <tr><td>Item4</tr><td>Item4 Again</td></tr>
>
> And so on. I want to search this html for a table row that has 'Item3' in
> the first cell. And the second cell should contain 'Item3 Again'. I know
> that I can do this with something like this:
>
> $file = fopen("http://localhost/html.html",
> "r");
> $data = fread($file, 10000);
> $results = eregi("<tr><td>Item3</tr><td>Item3
> Again</td></tr>", $data);
>
>
> However, this won't exactly work for me, because that contents of the
> second cell will change daily, so tomorrow, the third table row could look
> like this:
>
> <tr><td>Item3</td><td>Item3 Has Changed</td></tr>
>
> I was thinking I could use sscanf() to do this, but couldn't get anything
> to work. I've tried everything I can think of.
>
> Sorry if the explanation of my problem was hard to understand, I don't
> exactly understand it myself. Thanks everybody!!!
>
> Regards,
> Tyler Longren
>
You just need to use a regular expression with eregi:
<?php
$data = "<tr><td>Item3</td><td>Item3 Has
Changed</td></tr>";
eregi("<tr><td>([^>]+)</tr><td>([^>]+)</td></tr>",
$data, $regs);
echo $regs[1]; // "Item3"
echo $regs[2]; // "Item3 Has Changed"
?>
A good guide to regular expressions (which are absolutely indispensible if
you want to do much work with strings) is here:
http://www.delorie.com/gnu/docs/rx/rx_toc.html
Cheers
Simon Garner