Forum Moderators: coopster

Message Too Old, No Replies

Isolating variables from large string

         

otem

12:14 am on Mar 1, 2007 (gmt 0)

10+ Year Member



Hello,

I'm writing a script that will look at a string comprising of a page's source code to extract nine variables contained inside.

Right now I'm faced with the problem of trying to isolate the variables.

Let's say part of the source code looks like this:

...
<td class="class" width="175" height="20" align="right" valign="top">Class: Calculus</td>
<td>***</td>
<td class="class" width="175" height="20" align="right" valign="top">Grade: A</td>
<td>***</td>
<td class="class" width="175" height="20" align="right" valign="top">Year: 1988</td>
<td>***</td>
...

What would be the best approach to isolate the values "Calculus", "A" and "1988"?

I've been playing around with explode, but I don't think that's giving me the solution I need.

Thanks.

eelixduppy

2:01 am on Mar 1, 2007 (gmt 0)



Hello otem,

Looks like you are going to need regular expressions [us2.php.net] for this ;)

Try something like the following:


$pattern = "/<td[^>]+>([^<\/td>]+)<\/td>/i";
$matches = array();
if([url=http://us2.php.net/preg-match-all]preg_match_all[/url]($pattern,$string,$matches)) {
echo '<pre>';
[url=http://us2.php.net/manual/en/function.print-r.php]print_r[/url]($matches);
echo '</pre>';
}

Where

$string
is the HTML code you referenced above.

Good luck! :)

otem

12:49 am on Mar 2, 2007 (gmt 0)

10+ Year Member



Thanks so much!

Preg_match_all worked great and I now have a very basic understanding of a new tool I can stick onto my PHP tool belt!

Thanks.