Question : Regex to remove width property from HTML TAble tag where width is over 500

OK, so I have a php script that needs to process a number of strings of HTML.
Some of those strings contain table declarations with a width over 500. I want to remove that width.

So
<table width="561" cellspacing="0">

becomes

<table cellspacing="0">

and <table width="499"> is left as is.

Possible?

Answer : Regex to remove width property from HTML TAble tag where width is over 500

I think the safest approach would be to apply both conditions separately (but still in the same regex), as below. I modified some of the original pattern near the beginning as I realized it would not be safe for things like:

    <table border="0"><tr width="100">

The original pattern would have turned this into

    <table border="0"><tr>

This is just an arbitrary example. The corrected pattern below takes this into account as well as incorporating your changes.
1:
$result = preg_replace('/(<table[^>]+)(?:(?:width="?(?:\d{4,}|5\d\d)"?)|(?:width:\s*(?:\d{4,}|5\d\d)(?:px|pt|%|;)*))/', "$1", $string);
Random Solutions  
 
programming4us programming4us