WonderLand
WonderLand

Reputation: 5674

PHP - Convert octal/hexadecimal escape sequences

I'm not sure about the exact naming of this php functionality so if you have advice for a better title to this question is welcome.

The point is, I have some string written in this way "ge\164\x42as\145\x44\151\x72" and I would like to convert them in readable chars ( for example the above string value is "getBaseDir")

How can I accomplish this in a programmatically way using php ?

Update
Those strings are contained in a php source file that I would like to parse and clean in order to be more readable.

So I would appreciate a solution that provide me a way to parse and covert this string all at once ( for example with a regex ) ...

here a portion of the code so it is more easy to understand the scenario

 public function cmp($x74, $x7a)
    {
        $x76 = $this->x1c->x3380->{$this->xc1->x3380->xe269};
        $x12213 = "\x68\145\x6c\x70\x65\x72";
        $x11f45 = "\x67\x65\164\123t\x6fr\x65Co\156\146\x69\147";

        if ($x76(${$this->x83->x3380->{$this->x83->x3380->{$this->x83->x3380->xd341}}}) == $x76(${$this->x83->x336e->{$this->xc1->x336e->{$this->xc1->x336e->x8445}}})) {
            return 0;
        }
        return ($x76(${$this->x83->x334c->{$this->x83->x334c->x3423}}) < $x76(${$this->x83->x336e->{$this->xc1->x336e->{$this->xc1->x336e->x8445}}})) ? 1 : -1;
    }

Just to clear the above code is part of an extension that we legitimate purchase but we need to customize.

Upvotes: 0

Views: 2158

Answers (4)

Umar
Umar

Reputation: 17

Try This

<?php
$inputString = "ge\164\x42as\145\x44\151\x72";

// Replace escape sequences with their corresponding characters
$properFormatString = preg_replace_callback('/\\\\([0-7]{1,3})/', function ($matches) {
    return chr(octdec($matches[1]));
}, $inputString);

echo $properFormatString;
?>

Upvotes: 0

user1505555
user1505555

Reputation: 1

try this

$ret = print_r("\\012", true);

Upvotes: 0

WonderLand
WonderLand

Reputation: 5674

$string = '"\125n\141\x62\154\145\40to\x67\145\156e\x72\141t\145\x20\x74\x68e\40d\x61t\141\40\146\145\145d\x2e"';

\\ convert the octal into string
$string = preg_replace_callback('/\\\\([0-7]{1,3})/', function ($m) {
    return chr(octdec($m[1]));
}, $string);

\\ convert the hexadecimal part of the string
$string = preg_replace_callback('/\\\\x([0-9A-F]{1,2})/i', function ($m) {
    return chr(hexdec($m[1]));
}, $string);

on this particular situation I need to parse a full file content match all string delimited by "" and convert them, here the full solution

$content = file_get_contents($filepath);

// match all string delimited by ""
$content = preg_replace_callback("/(\".*?\")/s ", function ($m) {
    $string = $m[1];

    \\ convert the octal into string
    $string = preg_replace_callback('/\\\\([0-7]{1,3})/', function ($m) {
        return chr(octdec($m[1]));
    }, $string);

    \\ convert the hexadecimal part of the string
    $string = preg_replace_callback('/\\\\x([0-9A-F]{1,2})/i', function ($m) {
        return chr(hexdec($m[1]));
    }, $string);

    return $string;

}, $content);

Upvotes: 1

Pedro Lobito
Pedro Lobito

Reputation: 99041

How can I accomplish this in a programmatically way using php ?

You can simply echo it:

echo "ge\164\x42as\145\x44\151\x72";
//getBaseDir

Upvotes: 0

Related Questions