KBD

Keith Devens .com

Thursday, March 18, 2010 Flag waving
Don't be an asshole. – Jason Hoffman

Archive: November 22, 2005

← November 21, 2005November 23, 2005 →

Daily link icon Tuesday, November 22, 2005

uriescape

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.

← November 21, 2005November 23, 2005 →
March 2010
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 2 posts

Recent comments XML

I hate ASP.NET

I hate ASP... I was doing wonders​with PHP, then suddenly one of my​clients...

Johnies: Mar 17, 6:14am

Quantum physics and free will

I knew you were going to say that....

Tom Massey: Mar 15, 9:26pm

Generated in about 0.057s.

(Used 7 db queries)