I'm trying to strip non-alphanumeric characters from a string and limit the string to two words. i.e:
foo bar baz => foo-bar
boo * test => boo-test
te%st_foo => test-foo
So far I've got this:
$term = preg_replace('/[^A-Za-z0-9 ]/', '', $term);
$words = explode(" ", $term);
$generated = strtolower(implode(" ",array_splice($words,0,2)));
$term = preg_replace('!\s+!', '-', $term);
But going wrong somewhere, these are snippets I've just snapped together to try and get the results I'm after.
The problem is really if there is more than 1 space in a string etc.
Help appreciated :)
Thanks