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

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.