PHP问题与转义的十六进制字符

时间:2015-05-31 16:18:44

标签: php

我正在运行一些PHP代码并收到错误。

PHP Fatal Error:  Call to undefined function \\x66\\x6f\\x6f()

以下是代码:

function string2binary($string)
{
    $binary = '';
    $binarr = str_split( $string );

    for ( $i=0; $i < count( $binarr ); $i++ )
        $binary.=strlen(decbin(ord($binarr[$i])))<8?str_pad(decbin(ord($binarr[$i])),8,0,STR_PAD_LEFT):decbin(ord($binarr[$i]));

    return $binary;
}

function binary2string($binary)
{
    $string = '';
    $chars  = explode( ' ' , chunk_split( $binary , 8 , ' ' ) );

    for ( $i = 0; $i < count( $chars ); $i++ )
        if ( ! empty( $chars[$i] ) )
            $string .= chr( bindec( $chars[$i] ) );

    return $string;
}

function foo( $bar = 'bar' )
{
    echo $bar . '<br />';
}

$fn = "foo";

echo $fn . '<br />';

$fn();

$fn = "\x66\x6f\x6f";

echo $fn . '<br />';

$fn();

$fn = string2binary( "foo" );

echo $fn . '<br />';

$fn = binary2string( $fn );

echo $fn . '<br />';

$fn();

$fn = string2binary( '\x66\x6f\x6f' );

echo $fn . '<br />';

$fn = binary2string( $fn );

echo $fn . '<br />';

$fn();

输出

foo
bar
foo
bar
011001100110111101101111
foo
bar
010111000111100000110110001101100101110001111000001101100110011001011100011110000011011001100110
\x66\x6f\x6f
PHP Fatal Error:  Call to undefined function \\x66\\x6f\\x6f()

我不知何故需要将字符串视为十六进制序列而不是扁平字符串。这个错误意味着什么?

1 个答案:

答案 0 :(得分:1)

您必须使用stripcslashes

$fn = binary2string( $fn );
echo stripcslashes($fn) . '<br />';

使用该值调用该函数,如下所示:

function foo() { echo 'call'; }

$bar = '\x66\x6f\x6f';
$fooName = stripcslashes($bar);

echo $fooName . "\n";
$fooName();

演示:http://3v4l.org/CjBBm