我想删除<td>
元素中的空格。
在这个元素中,我有几个<span>
元素,它们之间是不必要的空格。有没有办法删除它们而不删除<span>
元素中的空格?
我没有可能在代码中删除它们,我将通过使用css或更改PHP输出来处理它们。
我有以下想法,但它删除了(当然)所有空格:
echo str_replace(' ', '', $total['value']);
我也尝试过CSS方法:
white-space: pre-line
但是这会在每个空间增加一条线......
还有其他想法吗?
修改 这就是我所拥有的:
<td class="totals totals_amount_order_total">
<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">€</span> 636,17</span>
<br class="wgm-break">
<span class="wgm-tax includes_tax">Enthält<span class="amount"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">€</span> 101,57</span></span> MwSt. (19%)</span>
</td>
我现在要删除标记之间的所有空格(但不删除标记内的文本):
<td class="totals totals_amount_order_total"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">€</span> 636,17</span><br class="wgm-break"><span class="wgm-tax includes_tax">Enthält<span class="amount"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">€</span> 101,57</span</span> MwSt. (19%)</span></td>
编辑2:
我试图用它们之前的空格替换所有<span>
标记。我尝试使用preg_replace
执行此操作,但我不确定如何为范围内的不同类设置通配符
答案 0 :(得分:1)
$new = preg_replace('/>\s*</','><',$orig);
适合我!从您的示例输入和输出中,我注意到您只删除了标记之间的空格。
我的测试中的完整代码:
$orig="<td class=\"totals totals_amount_order_total\">
<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">€</span> 636,17</span>
<br class=\"wgm-break\">
<span class=\"wgm-tax includes_tax\">Enthält<span class=\"amount\"><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">€</span> 101,57</span></span> MwSt. (19%)</span>
</td>";
$new = preg_replace('/>\s*</','><',$orig);
print($new);
我得到了这个(查看来源):
<td class="totals totals_amount_order_total"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">€</span> 636,17</span><br class="wgm-break"><span class="wgm-tax includes_tax">Enthält<span class="amount"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">€</span> 101,57</span></span> MwSt. (19%)</span></td>