I've been meaning to write a replacement for PHP's built-in urlencode so that characters that don't have to be escaped in the path part of a URI won't be. Here 'tis:
<?php
function uriescape($uri){
static $invalid_chars = '/([^-A-Za-z0-9_.!~*\'():@&=$, ])/e';
return str_replace(' ', '+', preg_replace($invalid_chars, '\'%\'.dechex(ord(\'$1\'))', $uri));
}
$uri = ' -_.!~*\'():@&=+$,';
var_dump(urlencode($uri));
var_dump(uriescape($uri));
?>
Gives:
string(43) "+-_.%21%7E%2A%27%28%29%3A%40%26%3D%2B%24%2C"
string(17) "+-_.!~*'():@&=%2b$,"
Note that this is only valid for the path component of the URI. In particular, it's not valid for a query string since the extra characters ":", "@", "&", "+", ",", and "$" are reserved there. Though "+" is interpreted as a space anyway, so I don't know what it means to say that it's reserved in the query and not a path segment. And I don't know why characters like "/" and "?" are reserved in the query as well.
Update: Fixed so that the '+' would still be escaped, as it needs to be, despite what the spec says, to not be interpreted as a space. Where did that convention of using a '+' to represent a space come from anyway? I didn't notice that specified anywhere in the URI spec.
new⇒Wizard's First Rule
> while it is cheesy to someextent, I actually found it to bepretty enjoy...
Keith: Jul 3, 6:33pm