Jump to content

preg_replace question


golles

Recommended Posts

I have a table with rows like this:

 

<tr>
<td>String</td>
<td>int</td>
<td>String</td>
<td>String</td>
<td>String</td>
<td class='aR'>int</td>
<td class='tbr'>String</td>
</tr>

 

If one of the Strings is $highlight (loaded from the database) I would like to change it to the following:

 

<tr class="highlight">
<td>String</td>
<td>int</td>
<td>String</td>
<td>String</td>
<td>String</td>
<td class='aR'>int</td>
<td class='tbr'>String</td>
</tr>

 

I hope you understand it now.

I don't think I'm really clear, sorry for that!

I think this topic belongs in PHP Regex forum...

 

this is an example of the input

<table class="mytable" cellspacing=0>
<tr><td>22-11-2009</td><td>5886</td><td>Hello</td><td>-</td><td>World</td><td class='aR'>16</td><td class='tbr'>-  11</td></tr>
<tr><td></td><td>6525</td><td>See</td><td>-</td><td>This</td><td class='aR'>6</td><td class='tbr'>-  4</td></tr>
<tr><td>22-11-2009</td><td>5886</td><td>Hello</td><td>-</td><td>Everyone</td><td class='aR'>16</td><td class='tbr'>-  11</td></tr>
<tr><td></td><td>6525</td><td>See</td><td>-</td><td>This</td><td class='aR'>6</td><td class='tbr'>-  4</td></tr>
</table>

 

Now i would like to change the output to this:

<table class="mytable" cellspacing=0>
<tr class="bgRed"><td>22-11-2009</td><td>5886</td><td>Hello</td><td>-</td><td>World</td><td class='aR'>16</td><td class='tbr'>-  11</td></tr>
<tr><td></td><td>6525</td><td>See</td><td>-</td><td>This</td><td class='aR'>6</td><td class='tbr'>-  4</td></tr>
<tr class="bgRed><td>22-11-2009</td><td>5886</td><td>Hello</td><td>-</td><td>Everyone</td><td class='aR'>16</td><td class='tbr'>-  11</td></tr>
<tr><td></td><td>6525</td><td>See</td><td>-</td><td>This</td><td class='aR'>6</td><td class='tbr'>-  4</td></tr>
</table>

 

The table rows with a cell with the text "Hello" should have a class (bgRed), so I can style the complete row.

 

How can this be done?

 

I hope this makes some more sense.

 

-golles

There are tonnes of ways of doing something like this, the following is just one example.

 

preg_replace('#^<tr>(?=.*?<td>Hello</td>)#m', '<tr class="bgRed">', $html);

Thanks!

 

Now it works almost perfect, if the table has a title, then it doesn't work, how can this be fixed?

 

thank you salathe, thanks!

 

-golles

Ooops, sorry...

 

preg_replace('#^(<tr[^>]*)>(?=.*?<td>Hello</td>)#m', "$1" . ' class="bgRed">', $html);

thanks, I just noticed that there was a title on the td to, but i fixed it myself (with your example!)

 

preg_replace("#^(<tr[^>]*)>(?=.*?(<td[^>]*)>Hello</td>)#m", "$1" . ' class="bgRed">', $html);

 

thanks cags and salathe!!!

 

I think I need some more experience with the regex stuff  :shy:

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.