CodeIgniter Forums

Full Version: Convert space to non-breaking space
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

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?