coder007 Posted June 9, 2009 Share Posted June 9, 2009 Hey! This is my RegEx (PCRE - preg): preg_match("/<td><center>([0-9,.]+)<\/td><\/center><td><center>([0-9,.]+)<\/td><\/center><td><center>([0-9,.]+)<\/td><\/center><td><center>([0-9])<\/td><\/center>/", $line, $matches); This is the haystack: <td><center>5/30/2009 3:07:08 AM</td></center><td><center>66<br>Days</td></center><td><center><img border="0" src="images/teams/team_Orange.gif" width="14" height="13" title="Team: Orange"> </td></center><td><center>125,633.273</td></center><td><center>15,604.47</td></center><td><center>9,627.72</td></center><td><center><img src='images/war.gif' border=0 title='War is an option'> </td></center></tr><tr bgcolor='#ffffff'> The values I am trying to get are the numbers, namely the date (5/30/2009 3:07:08 AM), the first number (125,633.273), the second number (15,604.47), and the third number (9627.72). Currently I am getting 0's for all but the date. Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/161467-not-getting-all-of-needle-out-of-haystack/ Share on other sites More sharing options...
Adam Posted June 9, 2009 Share Posted June 9, 2009 When dot is used literally it needs to be escaped with a backslash, i.e: ([0-9,\.]+) Quote Link to comment https://forums.phpfreaks.com/topic/161467-not-getting-all-of-needle-out-of-haystack/#findComment-852139 Share on other sites More sharing options...
nrg_alpha Posted June 9, 2009 Share Posted June 9, 2009 When dot is used literally it needs to be escaped with a backslash, i.e: ([0-9,\.]+) Not within a character class it doesn't. Almost all meta characters lose their special meaning within the class (the dot included). Quote Link to comment https://forums.phpfreaks.com/topic/161467-not-getting-all-of-needle-out-of-haystack/#findComment-852318 Share on other sites More sharing options...
nrg_alpha Posted June 9, 2009 Share Posted June 9, 2009 Note to OP... that chunk of code has improper nesting (which could make things a little trickier to navigate): <td><center>125,633.273</td></center> should be <td><center>125,633.273</center></td> So perhaps a quick and dirty way could be: $str = <<<HTML <td><center>5/30/2009 3:07:08 AM</td></center><td><center>66<br>Days</td></center><td><center><img border="0" src="images/teams/team_Orange.gif" width="14" height="13" title="Team: Orange"> </td></center><td><center>125,633.273</td></center><td><center>15,604.47</td></center><td><center>9,627.72</td></center><td><center><img src='images/war.gif' border=0 title='War is an option'> </td></center></tr><tr bgcolor='#ffffff'> HTML; preg_match('#((?:\d{1,2}/){2}\d{4}[^<]+).+?([\d,]+\.\d+).+?([\d,]+\.\d+)#', $str, $match); echo $match[1] . "<br />\n" . $match[2] . "<br />\n" . $match[3]; EDIT - The way I grab those two sets of numbers relies on the decimal to be involved. Again, this is a quick fast way with no fus.. as those tags are not ordered correctly in some spots, which makes me question the consistency of the code you are checking... Quote Link to comment https://forums.phpfreaks.com/topic/161467-not-getting-all-of-needle-out-of-haystack/#findComment-852328 Share on other sites More sharing options...
Adam Posted June 9, 2009 Share Posted June 9, 2009 Well, what a shocker! Didn't know that... Perhaps this will work better: preg_match('#<td><center>([^<]+)</td></center><td><center>66<br>Days</td></center><td><center><img border="0" src="images/teams/team_Orange.gif" width="14" height="13" title="Team: Orange"> </td></center><td><center>([^<]+)</td></center><td><center>([^<]+)</td></center><td><center>([^<]+)</td>#', $line, $matches); EDIT: Probs best off going with nrg_alpha's! Quote Link to comment https://forums.phpfreaks.com/topic/161467-not-getting-all-of-needle-out-of-haystack/#findComment-852329 Share on other sites More sharing options...
nrg_alpha Posted June 9, 2009 Share Posted June 9, 2009 I suppose I could have included say <td><center> at the start of my pattern to help ensure the matching of the appropriate date location (in the event there is other dates located on the page - would have been nice if those td tags had some ids or classes to help differentiate themselves though). Quote Link to comment https://forums.phpfreaks.com/topic/161467-not-getting-all-of-needle-out-of-haystack/#findComment-852332 Share on other sites More sharing options...
coder007 Posted June 9, 2009 Author Share Posted June 9, 2009 The code is not from my site, but in fact an input from another site. Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/161467-not-getting-all-of-needle-out-of-haystack/#findComment-852375 Share on other sites More sharing options...
coder007 Posted June 9, 2009 Author Share Posted June 9, 2009 Okay... So I replaced my current string with what you have, but the results array is still coming up with blank values for $match[1], $match[2], and $match[3]. Quote Link to comment https://forums.phpfreaks.com/topic/161467-not-getting-all-of-needle-out-of-haystack/#findComment-852388 Share on other sites More sharing options...
nrg_alpha Posted June 9, 2009 Share Posted June 9, 2009 What is the webpage you are trying to get this info off of? And is there multiple instances of these? Quote Link to comment https://forums.phpfreaks.com/topic/161467-not-getting-all-of-needle-out-of-haystack/#findComment-852393 Share on other sites More sharing options...
coder007 Posted June 9, 2009 Author Share Posted June 9, 2009 I sent you the html of the webpage via PM...I can't send you a link because it is passsword protected. NVM about HTML...content is too large for messages. And yes, there are multiple instances of these. Quote Link to comment https://forums.phpfreaks.com/topic/161467-not-getting-all-of-needle-out-of-haystack/#findComment-852402 Share on other sites More sharing options...
nrg_alpha Posted June 9, 2009 Share Posted June 9, 2009 Perhaps cutting and paste a 'small' portion of the code (containing one or two examples) if it differs from what you initially posted.. because I used that line of code you posted as a test, and it worked... so I'm thinking there might be some variances in the code that the pattern isn't taking into account? Quote Link to comment https://forums.phpfreaks.com/topic/161467-not-getting-all-of-needle-out-of-haystack/#findComment-852423 Share on other sites More sharing options...
coder007 Posted June 10, 2009 Author Share Posted June 10, 2009 I have sent 3 examples of said code. Thanks for your help once again. Quote Link to comment https://forums.phpfreaks.com/topic/161467-not-getting-all-of-needle-out-of-haystack/#findComment-852835 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.