How replace all spaces inside HTML elements with   using preg_replace?

Pro85 picture Pro85 · Mar 6, 2011 · Viewed 19.4k times · Source

I need replace spaces with   inside HTML elements. Example:

<table atrr="zxzx"><tr>
<td>adfa a   adfadfaf></td><td><br /> dfa  dfa</td>
</tr></table>

should become

<table atrr="zxzx"><tr>
<td>adfa&nbsp;a&nbsp;&nbsp;&nbsp;adfadfaf></td><td><br />&nbsp;dfa&nbsp;&nbsp;dfa</td>
</tr></table>

Answer

krtek picture krtek · Mar 6, 2011

If you're working with php, you can do

$content = str_replace(' ', '&nbsp;', $content);