Jump to content

preg_replace question


golles

Recommended Posts

Hello!!

 

I hope someone can help me with preg_replace.

 

I load two tables from an external website, now I would like to highlight a complete tr (background color) if there is a td with a known string in it.

 

So, how can I do this?

 

Thanks in advance!

 

-golles

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Try something like this..

 

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

 

This will return the following error:

Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$'

 

Hopefully you know a solution :-[

Link to comment
Share on other sites

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:

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.