2

I'm trying to convert some Javascript to PHP manually. The script I'm trying to convert is:

if (s)
    r += "\"" + s;
else
    r += "\"";

r += "\\\\\"+" + gv + "._+" + n.toString(16).replace(/[0-9a-f]/gi, function(c) {
    return gv + "." + b[parseInt(c, 16)] + "+"});
s = "";

What I have come up with is:

if ($s) {
    $r = $r . "\"" . $s;
} else {
    $r = $r . "\"";
}
$r = $r . "\\\\\"+" . str_replace([0-7],function($c, $GlobalVar, $b){
        return $GlobalVar.".".$b[$c]."+";
    },ord($n));

But I'm getting:

The localhost page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500

I'm assuming using a function in str_replace is breaking the script, but if I remove it I don't know if I can find the same functionality as the Javascript code. Any help is appreciated.

Php Log:

[Thu Aug 25 12:46:43.157926 2016] [:error] [pid 5864] [client 127.0.0.1:53936] PHP Catchable fatal error: Object of class Closure could not be converted to string in /var/www/html/html/test.php

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • 3
    You probably look for `preg_replace_callback()` with this you can use a regex as search and a callback function as replacement. – Rizier123 Aug 25 '16 at 12:01
  • Where would my error log be? (running Ubuntu) @PaulCrovella –  Aug 25 '16 at 12:05
  • See http://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php5-apache-fastcgi-cpanel for the error log location. – chris85 Aug 25 '16 at 12:28
  • @PaulCrovella I added the Apache error log –  Aug 25 '16 at 12:37
  • I think the problem is the return result of [str_replace](http://php.net/manual/en/function.str-replace.php) it cant be concatenated using "." apparently... – DIEGO CARRASCAL Aug 25 '16 at 12:53
  • So I think you're close on a good question here... you just need to post more of the JS. Specifically what `gv` and `b` are. `b` especially since you don't define it anywhere – Machavity Aug 25 '16 at 12:53
  • Who did upvote this question! – revo Aug 25 '16 at 13:26

2 Answers2

0

The error is simple, you should only read the error message:

 Object of class Closure could not be converted to string

The variable you are passing to the function is not a string.

And you can't use the same syntax of Javascript because it's different from PHP, you can't do a "literal conversion"

Giacomo Torricelli
  • 764
  • 1
  • 6
  • 21
0

The php function does something completely different to what the js function does.

Assuming the same values for gv and b, and n is an integer, the last line should be

$r = $r . "\\\\\"+$gv._+" . implode('', array_map(function($c) use ($b, $gv) {return $gv . "." . $b[hexdec($c)] . "+";}, str_split(dechex($n))));

or formatted for readability:

$r = $r . "\\\\\"+$gv._+" 
    . implode(
        '', 
        array_map(
            function($c) use ($b, $gv) {
                return $gv . "." . $b[hexdec($c)] . "+";
            }, 
            str_split(dechex($n))
        )
    );
Alex Blex
  • 34,704
  • 7
  • 48
  • 75