thales.pereira Posted April 6, 2011 Share Posted April 6, 2011 Hello all, I have the following extract of html output which repeats for every user: <TD valign=top><B>name</B></TD> <TD>santosani</TD> </TR> <TR> <TD valign=top><B>password</B></TD> <TD>**********************************</TD> </TR> <TR> <TD valign=top><B>membership</B></TD> <TD> <TABLE> <TR> <TD>Administrators</TD> </TR> <TR> <TD>Developers</TD> </TR> <TR> <TD>Everybody</TD> </TR> </TABLE> </TD> </TR> </TABLE> </TD> </TR> <TR> <TD><TABLE bgcolor=#dddddd border=1> What im trying to do, is extract the username and all the membership of that user. but to extract the memberhips i have to limit the search by getting everything between <TD valign=top><B>membership</B></TD> and <TD><TABLE bgcolor=#dddddd border=1> . The regex i have so far is: preg_match_all('#<TD valign=top><B>name</B></TD>\s<TD>(.*?)</TD>#i', $output, $match_user); Until now i only have the user and no clue about how i can get those fields. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
dcro2 Posted April 7, 2011 Share Posted April 7, 2011 Warning: might set your computer on fire (i.e. no guarantees, hacky code, but it works) if(preg_match("#<TD valign=top><B>name</B></TD>\s+<TD>([^\r\n]+)</TD>.+<TD valign=top><B>membership</B></TD>\s+<TD>\s+<TABLE>(.*)</TABLE>#isU", $string, $matches) && preg_match_all("#<TD>(.+)</TD>#iU", $matches[2], $groups) !== false) { $username = $matches[1]; $memberships = $groups[1]; } Quote Link to comment Share on other sites More sharing options...
thales.pereira Posted April 7, 2011 Author Share Posted April 7, 2011 set my computer on fire? lol, i want to see that testing it right now edit: awesome, it worked very well, but the code is only getting the first entrance of the user, how can i apply this solution for a repetition of 50 or more users? <TD valign=top><B>name</B></TD> <TD>santosani</TD> </TR> <TR> <TD valign=top><B>password</B></TD> <TD>**********************************</TD> </TR> <TR> <TD valign=top><B>membership</B></TD> <TD> <TABLE> <TR> <TD>Administrators</TD> </TR> <TR> <TD>Developers</TD> </TR> <TR> <TD>Everybody</TD> </TR> </TABLE> </TD> </TR> </TABLE> </TD> </TR> <TR> <TD><TABLE bgcolor=#dddddd border=1> thanks! Quote Link to comment Share on other sites More sharing options...
dcro2 Posted April 7, 2011 Share Posted April 7, 2011 if(preg_match_all("#<TD valign=top><B>name</B></TD>\s+<TD>([^\r\n]+)</TD>.*<TD valign=top><B>membership</B></TD>\s+<TD>\s+<TABLE>(.*)</TABLE>.*<TABLE bgcolor=\#dddddd border=1>#isU", $string, $matches, PREG_SET_ORDER)) { foreach($matches as $user) { $userinfo = array(); $userinfo['username'] = $user[1]; if(preg_match_all("#<TD>(.*)</TD>#iU", $user[2], $groups)) { $userinfo['groups'] = $groups[1]; } else { $userinfo['groups'] = array(); } $users[] = $userinfo; } } I didn't know how you wanted it, here's what the array looks like for two users: Array ( [0] => Array ( [username] => santosani [groups] => Array ( [0] => Administrators [1] => Developers [2] => Everybody ) ) [1] => Array ( [username] => santosani [groups] => Array ( [0] => Administrators [1] => Developers [2] => Everybody ) ) ) Quote Link to comment 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.