[SOLVED] howto extract data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
btfans
Forum Newbie
Posts: 22
Joined: Thu Jun 10, 2004 10:58 am

howto extract data

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

not testing..

Code: Select all

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

print_r($matches[1]);
btfans
Forum Newbie
Posts: 22
Joined: Thu Jun 10, 2004 10:58 am

Post 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.
btfans
Forum Newbie
Posts: 22
Joined: Thu Jun 10, 2004 10:58 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
btfans
Forum Newbie
Posts: 22
Joined: Thu Jun 10, 2004 10:58 am

Post 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 ?
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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;
}
?>
btfans
Forum Newbie
Posts: 22
Joined: Thu Jun 10, 2004 10:58 am

Post by btfans »

All,

Thanks very much.....
Post Reply