KBD

Keith Devens .com

Thursday, May 17, 2012 Flag waving
You can have premature generalization as well as premature optimization. – Bjarne Stroustrup

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 →
May 2012
SunMonTueWedThuFriSat
 12345
6789101112
13141516171819
20212223242526
2728293031 



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

Recent comments XML

new⇒Acknowledging the Arrival of Peak Government

In many ways, Peak Oil is​responsible for this new​uselessness of the big g...

Revence: May 16, 6:35am

new⇒Tab EXSPLOSION

Right now, I, too, have too many​tabs open. A rough count says​25.
Right. ...

Revence: May 16, 6:21am

George W. Bush: ‘I’m for Mitt Romney’ - ABC News

A marked difference (departure,​even) from the KBD of eight--yea,​even four...

Revence: May 15, 1:55pm

WebOb — WSGI request and response objects

Google App Engine forces one to​learn these....

Revence: May 15, 1:52pm

Generated in about 0.052s.

(Used 7 db queries)