update page now

Voting

: max(five, nine)?
(Example: nine)

The Note You're Voting On

jz
10 years ago
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) {
    // strip out all but unreserved characters from rfc:3986
    $stripped = preg_replace('/[^[:alnum:][:blank:]-._~]/', '', $string);
    // convert compacted whitespace to hyphens
    $slug = preg_replace('/[:blank:]+/', '-', $stripped);
    return $slug;
}
?>

<< Back to user notes page

To Top