删除 "width" in style 属性 inside table with preg_replace PHP

Delete "width" in style property inside table with preg_replace PHP

我有如下 HTML 代码:

<p style="text-align: center; width: 200px">Hellow world</p>
<table style="width: 500px; font-family: Arial; font-size: 18px">
  <td style="width: 20px; color: red">One</td>
  <td style="width: 50px; color: green">Two</td>
  <td style="width: 100px; color: blue">Three</td>
</table>

我需要 preg_replace PHP 的模式,它将仅从 所有标签[=29] 中删除宽度 属性 =] (td, tr, thead, tbody) 里面 table.

<p style="text-align: center; width: 200px">Hellow world</p>
<table style="font-family: Arial; font-size: 18px">
  <td style="color: red">One</td>
  <td style="color: green">Two</td>
  <td style="color: blue">Three</td>
</table>

到目前为止我想出了这个:

// deleting style attribute from table    
$sHtml = preg_replace('%<table[^>]*?style\s*=\s*"[^"]*"[^>]*>(.*?)</table>%si', '<table></table>', $sHtml);
// deleting style attribute from td
$sHtml = preg_replace('%<td[^>]*?style\s*=\s*"[^"]*"[^>]*>(.*?)</td>%si', '<td></td>', $sHtml);

删除 table 和 tds:

中的所有整个样式 属性
<p style="text-align: center; width: 200px">Hellow world</p>
<table>
   <td>One</td>
   <td>Two</td>
   <td>Three</td>
</table>

试试这个

// deleting style attribute from table    
$sHtml = preg_replace('/(<table|tr|td|tbody|thead)(.*)(width: [0-9]+px;)/i', '', $sHtml);