views:

22

answers:

2

In PHP, I want to compare two relative URLs for equality. The catch: URLs may differ in percent-encoding, e.g.

  • /dir/file+file vs. /dir/file%20file
  • /dir/file(file) vs. /dir/file%28file%29
  • /dir/file%5bfile vs. /dir/file%5Bfile

According to RFC 3986, servers should treat these URIs identically. But if I use == to compare, I'll end up with a mismatch.

So I'm looking for a PHP function which will accepts two strings and returns TRUE if they represent the same URI (dicounting encoded/decoded variants of the same char, upper-case/lower-case hex digits in encoded chars, and + vs. %20 for spaces), and FALSE if they're different.

I know in advance that only ASCII chars are in these strings-- no unicode.

+4  A: 
function uriMatches($uri1, $uri2)
{
    return urldecode($uri1) == urldecode($uri2);
}

echo uriMatches('/dir/file+file', '/dir/file%20file');      // TRUE
echo uriMatches('/dir/file(file)', '/dir/file%28file%29');  // TRUE
echo uriMatches('/dir/file%5bfile', '/dir/file%5Bfile');    // TRUE

urldecode

webbiedave
hah. what a simple, obvious solution! nice. +1
Justin Grant
A: 

EDIT: Please look at @webbiedave's response. His is much better (I wasn't even aware that there was a function in PHP to do that.. learn something new everyday)

You will have to parse the strings to look for something matching %## to find the occurences of those percent encoding. Then taking the number from those, you should be able to pass it so the chr() function to get the character of those percent encodings. Rebuild the strings and then you should be able to match them.

Not sure that's the most efficient method, but considering URLs are not usually that long, it shouldn't be too much of a performance hit.

Jared