Jump to content

Get Content Between two diferent HTML tags


thales.pereira

Recommended Posts

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.

 

 

 

 

Warning: might set your computer on fire (i.e. no guarantees, hacky code, but it works) :D

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];
}

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!

 

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
                )

        )

)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.