2015-05-05 44 views
0

我有一个非常简单的问题。我需要删除整个Php - Str_replace不识别空白

<td colspan="3" rowspan="1"> </td> 

标签从html只有当它的空(它不完全空,虽然,有某种空白)。试图使用:

$html = str_replace("<td colspan=\"3\" rowspan=\"1\"> </td>","",$html); 
    $html = str_replace("<td colspan=\"3\" rowspan=\"1\">&nbsp;</td>","",$html); 

没有成功。

preg_replace("/<td[^>]*>[\s|&nbsp;]*<\/td>/", '', $html); 

也没有工作。

它应该是一个相当简单的解决方案,有什么建议吗?

+2

试试这个'] *>([\ s] |  )? <\/td>' –

+0

它应该工作。你有什么收获? – Toto

+0

它只是不替代html。以前建议preg doesnt工作正常。 –

回答

0

也许这样?如果存在,它也将匹配多个空格。

<?php 

$result = preg_replace('#<td[^>]*>([\s]|&nbsp;)*<\/td>#i', '', $html); 

var_dump($result); 
+0

谢谢,这工作! –