How to force line-breaks on  ?

html, php, text

Solution

By definition, ` ` is a non-breakable space. It's very meaning is not to be broken across line endings. If this is not what you intend then I suggest fixing the HTML instead of trying to force the browser into non-standard behaviour.

Problem

Sometimes text on my pages looks very strange, real example: ``` trained&nbsp;professionals&nbsp;and&nbsp;paraprofessionals&nbsp;coming together ``` ...While the parent div is quite narrow so the text is just sticking out of it. And it looks quite strange, because `&nbsp;` actually represents a space. So, I wonder if it's possible to make the browser account these characters as actual spaces and break the line where necessary without actually replacing them? EDIT Why a blind replacing is a problem? Because `&nbsp;` may be needed sometimes. Consider the following example: ``` Ranks:<br> &nbsp;Marshall<br> &nbsp;&nbsp;Leutenant<br> &nbsp;&nbsp;&nbsp;Sergeant ``` If I just use a `preg_replace` on them it would look differently in the end. (I would also consider some suggestions if you have any ideas on replacing them smartly (for php platform) If you could think of some algorithm that wouldn't affect formatting.)

Original source