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.

 

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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
                )

        )

)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.