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

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Convert space to non-breaking space (/thread-30109.html)



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

[eluser]theprodigy[/eluser]
I need a good way to convert spaces to non-breaking spaces, but without screwing up html tags.

Example:
How would I convert
Code:
$string = '<p style="margin:0; color:#CCCCCC">This is a paragraph<p>';
into
Code:
$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?