Page 1 of 1

howto extract data

Posted: Thu Jun 10, 2004 10:58 am
by btfans
I have to extract the 7 ball_nn.gif (value of nn) from the following html, can any expert show me how to code?
I use eregi and can only get the first nn ? Thanks.
<table width="365" border="0" cellspacing="0" cellpadding="2" align="center">
<tr>
<td width='43'><div align='center'><img src='/images/mark6/largeballs/ball_07.gif' width='34' height='34'></div></td><td width='43'><div align='center'><img src='/images/mark6/largeballs/ball_18.gif' width='34' height='34'></div></td><td width='43'><div align='center'><img src='/images/mark6/largeballs/ball_28.gif' width='34' height='34'></div></td><td width='43'><div align='center'><img src='/images/mark6/largeballs/ball_29.gif' width='34' height='34'></div></td><td width='43'><div align='center'><img src='/images/mark6/largeballs/ball_37.gif' width='34' height='34'></div></td><td width='43'><div align='center'><img src='/images/mark6/largeballs/ball_49.gif' width='34' height='34'></div></td><td width='25'><div align='center'><b><font size='5'>+</font></b></div></td><td width='43'><div align='center'><img src='/images/mark6/largeballs/ball_12.gif' width='34' height='34'></div></td>
</tr>
</table>

Posted: Thu Jun 10, 2004 11:08 am
by feyd
not testing..

Code: Select all

preg_match_all('|<img.*?src\s*?=.*?(ball_\d+\.gif)|is',$allLines,$matches);

print_r($matches[1]);

Posted: Thu Jun 10, 2004 11:17 am
by btfans
Hi, good.... can U explain a little bit (I am newbie) yr lines and now the result is
Array ( [0] => ball_07.gif [1] => ball_18.gif [2] => ball_28.gif [3] => ball_29.gif [4] => ball_37.gif [5] => ball_49.gif [6] => ball_12.gif )
Can I get exactly the 7 numbers in a string ??
i.e. $result = "07 18 28 29 37 49 12"

Thanks.

Posted: Thu Jun 10, 2004 11:19 am
by btfans
Or
$result = "07 18 28 29 37 49 + 12"
A bit trickly : there is a "+" before the last ball_12.gif .......

Many Thanks.

Posted: Thu Jun 10, 2004 11:23 am
by feyd
sure..

Code: Select all

preg_match_all('|<img.*?src\s*?=.*?ball_(\d+)\.gif|is',$allLines,$matches);

$matches = implode(' ',$matches[1]);
echo $matches;
[php_man]preg_match_all[/php_man]() is a special function to match against patterns.. it's a bit complicated to explain in a short amount of time..

[php_man]print_r[/php_man]() in the previous post prints out variables. It's great for debugging arrays and objects..

[php_man]implode[/php_man]() flattens an array (if possible) using the first argument (a space in this instance) to glue the array together into a string.

Posted: Thu Jun 10, 2004 11:33 am
by btfans
feyd,

You are great, mainly correct:
07 18 28 29 37 49 12
If I need ltrim leading zero of each number (if nn < 10) and add "+" before last number , any hint ?

Posted: Thu Jun 10, 2004 12:23 pm
by redmonkey

Code: Select all

<?php
if (preg_match_all('/\/ball_(\d{2,})\.gif/', $allLines, $matches))
{
  array_walk($matches[1], create_function('&$in', 'if ($in{0} == "0") $in = substr($in, 1);'));
  
  $bonus    = array_pop($matches[1]);
  $main     = implode(' ', $matches[1]);
  $combined = $main . ' + ' . $bonus;

echo $combined;
}
?>

Posted: Thu Jun 10, 2004 1:00 pm
by btfans
All,

Thanks very much.....