I am using FCKeditor to paste spreadsheets from Excel and then export all of them to Word. In order to make the table fit in Word, I need to remove some of the cell settings.

How do I find multiple instances of width and delete them? It can be set as

width="12"

or

style="width: 12pt;"

They can have any combination of numbers, such as 12, 26, 132, etc. Plus they can always be different, so I can't set static numbers to look for every time.

    Well you could remove ALL set widths by doing:

    $data = preg_replace('/(?:width="[0-9]+")|(?:style="width: [0-9]+pt;")/', '', $data);

    Note that the ereg*() functions have long since been deprecated in favor of the [man]PCRE[/man] function library.

      The code didn't throw any errors, but the page still comes up with set widths throughout.

        Well then you'll have to adjust the syntax for the various styles of widths in the code, because this:

        $data = <<<EOD
        <tr width="12">
        	<td style="width: 5pt;">This is some sample data!</td>
        	<td style="width: 8pt;">It has fixed-widths!</td>
        </tr>
        EOD;
        
        echo preg_replace('/(?:width="[0-9]+")|(?:style="width: [0-9]+pt;")/', '', $data);

        produced the correct result of:

        <tr >
        	<td >This is some sample data!</td>
        	<td >It has fixed-widths!</td>
        </tr>

          I have no doubt they work, but my widths have 2-3 digit numbers. I tried putting in a range from 0-9999 (I know - too much). Is there a range limit with the numbers? Unfortunately, I didn't try with just 2 or 3 digits. That's why I'm here! 🙂

            The [0-9]+ in the pattern means "match any character from 0 to 9 one or more times", so no, it doesn't matter if it's "9" or "99" or "9999999999999999999999999999999999." There must be some other minor change in the formatting.

              That's weird. I wonder why it didn't work. When I get back to work tomorrow, I'll post the code that gets dumped by FCKeditor and show you how I'm "attemptin" to run your code against it. Thanks so much for your help!

                Does FCKEditor provide an interface for adding additional filters? If it does, maybe you could use that.

                  bradgrafelman;10879819 wrote:

                  Well then you'll have to adjust the syntax for the various styles of widths in the code, because this:

                  $data = <<<EOD
                  <tr width="12">
                  	<td style="width: 5pt;">This is some sample data!</td>
                  	<td style="width: 8pt;">It has fixed-widths!</td>
                  </tr>
                  EOD;
                  
                  echo preg_replace('/(?:width="[0-9]+")|(?:style="width: [0-9]+pt;")/', '', $data);

                  produced the correct result of:

                  <tr >
                  	<td >This is some sample data!</td>
                  	<td >It has fixed-widths!</td>
                  </tr>

                  Thanks! I went into work today and broke down the code so I could make sure I was doing everything right (cuz I'm an idiot) and it worked fine.

                    Write a Reply...