KBD

Keith Devens .com

Thursday, September 9, 2010 Flag waving
I meant what I said, and I said what I meant. An elephant's faithful, one hundred percent! – Horton (Horton Hears a Who, Dr. Seuss)

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 →
September 2010
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
2627282930 



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⇒Hawking on God

It is not lost on me that, in​saying “The universe created​itself,” esp...

Revence: Sep 9, 2:33am

new⇒Johnny Walker Blue Label

its good to see as many people out​there share the love of johnnie as​i do,...

jared aus - nz: Sep 8, 11:15am

Spider solitaire

 It is a spider solitaire game but​It's not this one, completely​different....

Pat: Sep 5, 9:37pm

Top Ten Bible Versions: Revisited (2010) | This Lamp

NIV is good and reads well but I​always felt like there should be​better. T...

Keith: Sep 5, 4:25pm

Generated in about 0.088s.

(Used 7 db queries)