For those looking to strip all non-reserved characters from a URL according to RFC 3986, the code would look like:
<?php
$stripped = preg_replace('/[^[:alnum:]-._~]/', '', $source);
?>
To get this string as it should be used in a url, you still probably want to use rawurlencode, as the [:alpha:] posix bracket expression will catch accented characters - use [A-Za-z][0-9] instead if you only want to include ascii characters.
So a basic "slug" generation routine might look like:
<?php
function strtoslug($string) {
$stripped = preg_replace('/[^[:alnum:][:blank:]-._~]/', '', $string);
$slug = preg_replace('/[:blank:]+/', '-', $stripped);
return $slug;
}
?>