KBD

Keith Devens .com

Friday, May 16, 2008 Flag waving
If you rely on scale up, you’ll probably get killed. You should always be relying on scale out. – Phil Smoot

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 2008
SunMonTueWedThuFriSat
 123
45678910
11121314151617
18192021222324
25262728293031



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

Recent comments XML

"IMDB for music"

IMDB for Music? It looks to be a​couple of years old...​http://MusicTell.co...

Ken Empie: May 14, 9:57pm

Generated in about 0.041s.

(Used 7 db queries)