Page 1 of 1

Table Stripping Regex

Posted: Wed May 13, 2009 6:02 pm
by GeoBear
I think I've seen software advertised that will strip table tags. In other words, every tag that looks like any of the following:

<tr bgcolor="FFFFFF">, <TR valign=Top>, <tr width: 75% FONT=Arial, Verdana, valign=top>

...will be reduced to a simple <tr>. Of course, you can do the same thing with a table cell tag - <td> (I think).

I have three questions...

1. What's the basic regex used in this operation? I assume there's a relatively simple regex script that can be used to strip any kind of tag, simply replace <table> with <tr> or <td>.

2. Is there a way to strip ALL table tags - <table>, <tr> and <td> in one operation?

3. Once my table tags have all been stripped, is there a regex script that will let me add an attribute to every other table row?

For example, suppose my table looks like this:

Code: Select all

 
<table>
  <tr>
    <td>(Some text)</td>
  </tr>
  <tr>
    <td>(More text)</td>
  </tr>
</table>
 
How would I convert it to this?:

Code: Select all

 
<table>
  <tr>
    <td>(Some text)</td>
  </tr>
  [color=#FF0000]<tr class="even">[/color]
    <td>(More text)</td>
  </tr>
</table>
 
Thanks.

P.S. Is your search function working properly? I typed in strip table tags, and it ignored "table." A similar search for stripping <tr> tags ignored <tr>.

Re: Table Stripping Regex

Posted: Thu May 14, 2009 3:40 am
by prometheuzz
GeoBear wrote:I think I've seen software advertised that will strip table tags. In other words, every tag that looks like any of the following:

<tr bgcolor="FFFFFF">, <TR valign=Top>, <tr width: 75% FONT=Arial, Verdana, valign=top>

...will be reduced to a simple <tr>. Of course, you can do the same thing with a table cell tag - <td> (I think).

I have three questions...

1. What's the basic regex used in this operation? I assume there's a relatively simple regex script that can be used to strip any kind of tag, simply replace <table> with <tr> or <td>.

2. Is there a way to strip ALL table tags - <table>, <tr> and <td> in one operation?
Something like this:

Code: Select all

$text = '<tr bgcolor="FFFFFF">, <TR valign=Top>, <tr width: 75% FONT=Arial, Verdana, valign=top>';
echo preg_replace('/<(\S+)[^>]+>/', '<$1>', $text);
GeoBear wrote:3. Once my table tags have all been stripped, is there a regex script that will let me add an attribute to every other table row?
...
No. At least, not in an easy way: regex is not well suited for "counting" purposes. I could craft some solution in regex, but it will be a rather difficult thing and hard to maintain by someone without a solid knowledge of regex.

Re: Table Stripping Regex

Posted: Thu May 14, 2009 8:19 am
by GeoBear
Thanks, but I'm a little confused. Is your code something I can paste into Dreamweaver's search-and-replace function, or is this a PHP script? If it's a PHP script, then I'm not sure how to apply it.

I often use that same echo value - $text - to display articles stored in databases, but in your example, $text just equals three table row tags. I don't understand where the rest of the article - the text or table elements that aren't changed - fit in.

Or is this sort of a supplemental script? For example, would I display an article with an echo value like $Article, then define all the table row tags in that article as $text and use your script to strip them?

Re: Table Stripping Regex

Posted: Fri May 15, 2009 3:35 am
by prometheuzz
GeoBear wrote:Thanks, but I'm a little confused. Is your code something I can paste into Dreamweaver's search-and-replace function, or is this a PHP script? If it's a PHP script, then I'm not sure how to apply it.
...
Yes, it's a PHP code snippet/script. The actual regex (and the regex-replacement-string) is in the preg_replace(...) method.
But as I recommended in your other thread: I suggest you do a some basic tutorials first before delving into regex.

Good luck.