Strip non alphanumeric characters and limit string to two words
<p>I'm trying to strip non-alphanumeric characters from a string and limit the string to two words. i.e:</p>

<pre><code>foo bar baz    =&gt; foo-bar
boo   * test   =&gt; boo-test
te%st_foo      =&gt; test-foo
</code></pre>

<p>So far I've got this:</p>

<pre><code>$term = preg_replace('/[^A-Za-z0-9 ]/', '', $term);
$words = explode(" ", $term);
$generated = strtolower(implode(" ",array_splice($words,0,2)));
$term = preg_replace('!\s+!', '-', $term);
</code></pre>

<p>But going wrong somewhere, these are snippets I've just snapped together to try and get the results I'm after.</p>

<p>The problem is really if there is more than 1 space in a string etc.</p>

<p>Help appreciated :)</p>

<p>Thanks</p>
