Convert space to non-breaking space

I need a good way to convert spaces to non-breaking spaces, but without screwing up html tags.

How would I convert
$string = '<p style="margin:0; color:#CCCCCC">This is a paragraph<p>';
$string = '<p style="margin:0; color:#CCCCCC">This&nbsp;is&nbsp;a&nbsp;paragraph<p>';

I know this is a bad example as you would never do that with a paragraph, but it's just an example.
I want to change all the spaces of text into non-breaking spaces, but leaving inside the tags as normal, so as not to screw up the html.

Is it just a matter of preg_replace, or is there an easier way?

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.