Jump to content

Print Line after Pattern, but not the Pattern


thales.pereira

Recommended Posts

Hello everyone,

 

Im new on regex and im blocking my brain with this one...

 

i have a php file wich extract an html page seeking for every entry which is located bellow the tag <TD valign="top"><B>name</B></TD>

 

for example:

 

<TD valign="top"><B>name</B></TD>

<TD>Administrators</TD>

</TR>

<TR>

<TD valign="top"><B>inUse</B></TD>

<TD>true</TD>

</TR>

<TR>

<TD valign="top"><B>allow</B></TD>

 

<TD valign="top"><B>name</B></TD>

<TD>Users</TD>

</TR>

<TR>

<TD valign="top"><B>inUse</B></TD>

<TD>true</TD>

</TR>

<TR>

<TD valign="top"><B>allow</B></TD>

 

<TD valign="top"><B>name</B></TD>

<TD>Developers</TD>

</TR>

<TR>

<TD valign="top"><B>inUse</B></TD>

<TD>true</TD>

</TR>

<TR>

<TD valign="top"><B>allow</B></TD>

 

 

 

Until now, im only able seek the lines with:

 

  $html = strip_tags(file_get_contents("http://$iUser:$iPass@$iHost/invoke/wm.server.access/aclList"));

  preg_match_all('/(name.*\n.*)/', substr($html,3), $matches);

  print_r ($matches)

 

 

##### OUTPUT ######

Array

(

  [ 0 ] => Array

        (

            [ 0 ] => name

Administrators

            [ 1 ] => name

Users

            [ 2 ] => name

Developers

        )

)

 

 

but... my desirable output is:

 

##### OUTPUT ######

Array

(

    [ 0 ] => Array

        (

            [ 0 ] => Administrators

            [ 1 ] => Users

            [ 2 ] => Developers

        )

)

 

 

If anyone could help on this, im paying a  virtual beer :)

 

 

 

try

<?php
$test = '<TD valign="top"><B>name</B></TD>
<TD>Administrators</TD>
</TR>
<TR>
<TD valign="top"><B>inUse</B></TD>
<TD>true</TD>
</TR>
<TR>
<TD valign="top"><B>allow</B></TD>

<TD valign="top"><B>name</B></TD>
<TD>Users</TD>
</TR>
<TR>
<TD valign="top"><B>inUse</B></TD>
<TD>true</TD>
</TR>
<TR>
<TD valign="top"><B>allow</B></TD>

<TD valign="top"><B>name</B></TD>
<TD>Developers</TD>
</TR>
<TR>
<TD valign="top"><B>inUse</B></TD>
<TD>true</TD>
</TR>
<TR>
<TD valign="top"><B>allow</B></TD>';
preg_match_all('/<TD valign="top"><B>name<\/B><\/TD>.*?<TD>([^<]+)<\/TD>/is', $test, $out);
print_r($out[1]);
?> 

Sure do. Your results are in $matches[1], remember that. $matches[0] is the full captured pattern, including characters you don't want.

 

I tried your regex and it doesn't seem to work so I changed it a a litte. Also you don't need to strip tags before regex, do it after. Try to print_r the array now.

 

$html = file_get_contents("http://$iUser:$iPass@$iHost/invoke/wm.server.access/aclList");
preg_match_all('#name</B></TD>\s<td>([^<]+)#i', $html, $matches);
print_r($matches[1]);

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.