1

I have an input like this:

$input = 'GFL/R&D/50/67289';

I am trying to get to this:

GFL$2fR$26D$2f50$2f67289

So far, the closest I have come is this:

echo filter_var($input, FILTER_SANITIZE_ENCODED, FILTER_FLAG_ENCODE_LOW)

which produces:

GFL%2FR%26D%2F50%2F67289

How can I get from the given input to the desired output and what sort of encoding is the result in?

By the way, please note the case sensitivity going on there. $2f is required rather than $2F.

bcmcfc
  • 25,966
  • 29
  • 109
  • 181
  • no idea, but worst case you could try a simple `str_replace('%', '$', $str)` – Marc B Oct 29 '14 at 15:22
  • @MarcB it won't work because of the case sensitivity issue - filter var uppercases the result so I'm not sure I could reliably find the parts of the result that are encoded values in order to lowercase just those parts. – bcmcfc Oct 29 '14 at 15:25

1 Answers1

2

This will do the trick: url-encode, then lower-case the encoded sequences and swap % for $ with a preg callback (PHP's PCRE doesn't support case-shifting modifiers):

$input = 'GFL/R&D/50/67289';
echo preg_replace_callback('/(%)([0-9A-F]{2})/', function ($m) {
    return '$' . strtolower($m[2]);
}, urlencode($input));

output:

GFL$2fR$26D$2f50$2f67289
Synchro
  • 35,538
  • 15
  • 81
  • 104
  • Although I am still curious as to whether it was a particular encoding that was being used, or just a bastardised version of urlencode. – bcmcfc Nov 09 '14 at 12:31