4

I try to recreate a URL but can not find the encoding function

Edit:
background: the string is for an API (with no documentation ofcourse), I tried to use rawurlencode but it seems that only the original encode / decode functions work so I have to find what it is ...

$input = "2/3*( ((word*888) )>(word2 AND ((word3*888))/word4>888"; $encoded = "2/3*(%20((word*888)%20)%3E(word2%20AND%20((word3*888))/word4%3E2888"; 

Tried with urlendode, rawurlencode but they replace the brackets and "*"..

The string is created for URL from PHP (possibly Javascript).

Edit
base64_encode - destroys the string ..
htmlentities - is not making changes ..

Not looking for "best url encode function" .. just need to recreate the example without custom replace function.

Edit3
OK I did some more debugging and this is the most of the replace table:

% - %25
^ - %5E
+ - %2B
{ - %7B
[ - %5B
} - %7D
] - %5D
" - %22
| - %7C
\ - %5C
< - %3C
> - %3E
" " - %20

May be some sort of escape for SQL, the string is later transformed to a query.

10
  • Is that an SQL WHERE clause? Commented Nov 5, 2013 at 8:02
  • You can always use base64_encode. Commented Nov 5, 2013 at 8:04
  • i am smelling huge trouble down this path Commented Nov 5, 2013 at 8:07
  • phpfiddle.org/lite/code/ab7-ruz check this it works Commented Nov 5, 2013 at 8:08
  • I think you may have your own reasons to keep the slash, brackets and asterisk. Thus you may just need a function to make use of a group of str_replace. For example, to replace space with %20, and > with %3E. Commented Nov 5, 2013 at 8:12

1 Answer 1

1

You're looking for a PHP implementation of JavaScript encodeURI() function. Here we go:

function encodeURI($uri) { return preg_replace_callback("{[^0-9a-z_.!~*'();,/?:@&=+$#]}i", function ($m) { return sprintf('%%%02X', ord($m[0])); }, $uri); } 

And now you have exactly what you want.

Demo: https://eval.in/65314

Sign up to request clarification or add additional context in comments.

2 Comments

good catch with js:encodeURL() seems as the original function. I found that the problem was in fact in another part of the URL ... but will accept your answer as it is good answer for the OP.
@d.raev Thank you! Hope it can be helpful for everyone! :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.