bcmcfc
bcmcfc

Reputation: 26755

What is this encoding and how can I encode a string to it in PHP?

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.

Upvotes: 1

Views: 307

Answers (1)

Synchro
Synchro

Reputation: 37710

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

Upvotes: 2

Related Questions