CodeIgniter Forums
Convert space to non-breaking space - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived General Discussion (
+--- Thread: Convert space to non-breaking space (/thread-30109.html)

Convert space to non-breaking space - El Forum - 05-03-2010

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?