Jump to content

Remove Table Row by ID tag


paolo

Recommended Posts

Hello people,

 

i have a small problem with regex...  :(

 

 

1. I have a string which contains HTML code of some Table Rows in this format:

<tr id="systemhalfTableRow_(A|B)[NUMBER]">...dynamic content...</tr>

 

Now i need to remove this table row from the string with all the other rows in it.

 

2. Lets say this is my string with my rows

$tableRows = '<tr id="systemhalfTableRow_A1"><td>Hello</td></tr>
<tr id="systemhalfTableRow_B12"><td>Hello</td></tr>
<tr id="systemhalfTableRow_A2"><td>Hello</td></tr>';

 

3. I want to delete the rows with the id A from 1 to 2

for($i = 1; $i <= 2; $i++) {
$id = 'systemhalfTableRow_A' . $i;
preg_replace(PATTERN, '', $tableRows);
}

 

4. What should remain after that?

<tr id="systemhalfTableRow_B12"><td>Hello</td></tr>

 

I would like to use PREG

 

Please helf me with this one :)

Thank you!

 

greetings,

paolo

Link to comment
https://forums.phpfreaks.com/topic/244497-remove-table-row-by-id-tag/
Share on other sites


$tableRows = '<tr id="systemhalfTableRow_A1"><td>Hello</td></tr>
<tr id="systemhalfTableRow_B12"><td>Hello</td></tr>
<tr id="systemhalfTableRow_A2"><td>Hello</td></tr>';

$tableRows = preg_replace('~<tr id="systemhalfTableRow_A[1-2]">.*?</tr>~','',$tableRows);

 

However...this isn't really perfect... if you have nested tables within this, it's not going to match right.  You should really be using DOM to parse html, instead of regex.

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.