golles Posted November 23, 2009 Share Posted November 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/182656-preg_replace-question/ Share on other sites More sharing options...
cags Posted November 23, 2009 Share Posted November 23, 2009 It's difficult to know what your asking. Give an example input and an example output that conform to your requirements. Quote Link to comment https://forums.phpfreaks.com/topic/182656-preg_replace-question/#findComment-964030 Share on other sites More sharing options...
golles Posted November 23, 2009 Author Share Posted November 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182656-preg_replace-question/#findComment-964041 Share on other sites More sharing options...
golles Posted November 23, 2009 Author Share Posted November 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/182656-preg_replace-question/#findComment-964127 Share on other sites More sharing options...
salathe Posted November 23, 2009 Share Posted November 23, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/182656-preg_replace-question/#findComment-964132 Share on other sites More sharing options...
golles Posted November 23, 2009 Author Share Posted November 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/182656-preg_replace-question/#findComment-964160 Share on other sites More sharing options...
cags Posted November 23, 2009 Share Posted November 23, 2009 Try something like this.. preg_replace('#^(<tr[^>]*)>(?=.*?<td>Hello</td>)#m', $1 . ' class="bgRed">', $html) Quote Link to comment https://forums.phpfreaks.com/topic/182656-preg_replace-question/#findComment-964169 Share on other sites More sharing options...
golles Posted November 23, 2009 Author Share Posted November 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/182656-preg_replace-question/#findComment-964184 Share on other sites More sharing options...
cags Posted November 23, 2009 Share Posted November 23, 2009 Ooops, sorry... preg_replace('#^(<tr[^>]*)>(?=.*?<td>Hello</td>)#m', "$1" . ' class="bgRed">', $html); Quote Link to comment https://forums.phpfreaks.com/topic/182656-preg_replace-question/#findComment-964186 Share on other sites More sharing options...
golles Posted November 23, 2009 Author Share Posted November 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/182656-preg_replace-question/#findComment-964207 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.