Lytheum Posted June 4, 2006 Share Posted June 4, 2006 I need helping using this function to find some numbers out of a string of html. The html is the following:[code]<div class="mainscroll-content"><table cellpadding="3" cellspacing="0" border=0><tr><td align="left"><a href="overall.ws?table=0&user=zezima">Overall</a></td> <td align="right">2</td> <td align="right">2164</td> <td align="right">554,475,897</td></tr></table></div>[/code]Now, I need to find out what is between the first <td></td>, 2nd, 3rd, and fourth - all dynamically (I don't know the numbers yet, this is an example.This is how I have started it:[code]preg_match_all ("/<div class=\"mainscroll-content\">([^`]*?)<\/div>/", $data, $matches);// Loop through each content itemforeach ($matches[0] as $match) { // Stat preg_match ("/<a href=\"overall.ws?table=0&user= . $user\">([^`]*?)<\/a><\/td>", $match, $temp); $stat = $temp['1']; $stat = strip_tags($stat); $stat = trim($stat); // Rank preg_match ("/<td align=\"right\">([^`]*?)<\/td>/", $match, $temp); $rank = $temp['1']; $rank = trim($rank); // Level preg_match ("/<td align=\"right\">([^`]*?)<\/td>/", $match, $temp); $level= $temp['1']; $level = trim($level); // Exp preg_match ("/<td align=\"right\">([^`]*?)<\/td>/", $match, $temp); $exp = $temp['1']; $exp = trim($exp);[/code]It works..almost. It finds what is between the first <td></td>. for each different one I called. So it all finds the same data (duh).Sorry for the long code, and if I explained it wrong I will retry tomorrow (1am). Quote Link to comment https://forums.phpfreaks.com/topic/11135-preg_match/ Share on other sites More sharing options...
poirot Posted June 4, 2006 Share Posted June 4, 2006 Changed your regular expression, and now using preg_match_all:[code]<?php$str = '<td align="left"><a href="overall.ws?table=0&user=zezima">Overall</a></td><td align="right">2</td><td align="right">2164</td><td align="right">554,475,897</td>';$pattern = '/<td(?:[^>])*>(.*?)<\/td>/s';preg_match_all($pattern, $str, $m);$temp = array_map('trim', $m[1]);echo '<pre>';print_r($temp);echo '</pre>';?>[/code]Which outputs[code]Array( [0] => <a href="overall.ws?table=0&user=zezima">Overall</a> [1] => 2 [2] => 2164 [3] => 554,475,897)[/code] Quote Link to comment https://forums.phpfreaks.com/topic/11135-preg_match/#findComment-41640 Share on other sites More sharing options...
Lytheum Posted June 4, 2006 Author Share Posted June 4, 2006 Thanks so much. Exactly what I needed.-solved- Quote Link to comment https://forums.phpfreaks.com/topic/11135-preg_match/#findComment-41695 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.