note 116839 added to function.rawurlencode
| From: | jz | Date: | Sun, 08 Mar 2015 23:26:27 +0000 |
| Subject: | note 116839 added to function.rawurlencode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201891@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 173.166.142.162
----
Manual Page -- http://php.net/manual/en/function.rawurlencode.php
Edit -- https://master.php.net/note/edit/116839
Del: integrated -- https://master.php.net/note/delete/116839/integrated
Del: useless -- https://master.php.net/note/delete/116839/useless
Del: bad code -- https://master.php.net/note/delete/116839/bad+code
Del: spam -- https://master.php.net/note/delete/116839/spam
Del: non-english -- https://master.php.net/note/delete/116839/non-english
Del: in docs -- https://master.php.net/note/delete/116839/in+docs
Del: other reasons-- https://master.php.net/note/delete/116839
Reject -- https://master.php.net/note/reject/116839
Search -- https://master.php.net/manage/user-notes.php