Replacing the last occurrence of a character in a string
<p>I have a little issue I'm trying to find a solution for. </p>

<p>Basically, imagine you have the following string:</p>

<pre><code>    $string = 'Hello I am a string';
</code></pre>

<p>And you'd like it to end with something like the folowing:</p>

<pre><code>    $string = 'Hello I am a&amp;nbsp;string';
</code></pre>

<p>Simply, replacing the last occurrence of a space, with a non-breaking space.</p>

<p>I'm doing this because I don't want the last word in a heading to be on its own. Simply because when it comes to headings:</p>

<pre><code> Hello I am a
 string
</code></pre>

<p>Doesn't look as good as</p>

<pre><code> Hello I am
 a string
</code></pre>

<p>How does one do such a thing?</p>
