Need help writing a string pattern

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
curlyroger
Forum Newbie
Posts: 2
Joined: Tue Jun 25, 2002 1:49 pm

Need help writing a string pattern

Post by curlyroger »

I am trying to use ereg_repalce to get rid of the LAST column from an html table, but cannot figure out the proper string pattern syntax.

The beginning of the pattern is "<td width". Since this is the last column I want to delete, I would use "</tr>" as the ending part.

I tried using "<td w([^>]|\n)*tr>" but that didn't work. I think I am getting problems with the carriage returns in the HTML and the extra < and > too.

So,

Code: Select all

...
    </td>
    <td width="19%" valign="TOP"> 
      <b>ANY HTML HERE</b>
    </td>
  </tr>
  <tr>
...

would become

Code: Select all

...
    </td>
    <!-- LAST COLUMN DELETED -->
  </tr>
  <tr>
...
Thanks!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

found this working (but only tested a little)

Code: Select all

<?php
$string = "<table border='1'><tr><td>test1</td><td>test2</td>\n<td>test3</td>\n\n</tr></table>";
$pattern = ';(<td>&#1111;^<>]*</td>(?!<td>)&#1111;\s$]*</tr>);m';
$string = preg_replace($pattern, '</tr>', $string);
print($string);
?>
curlyroger
Forum Newbie
Posts: 2
Joined: Tue Jun 25, 2002 1:49 pm

Post by curlyroger »

Thanks! That is soooooo close to what I need. However, this does not work if you put any html tags inside the test3 box. Meaning:

....<td>test3<br>html tags <b>don't</b> work</td>\n....

That doesn't work because of the tags. Is there anyway to modify the string pattern to ignore those?
Post Reply