使用PHP中的单引号将ASCII代码转换为UTF-8

I've a problem with encoding. The output I want is:

string(17) "New & old"

This is also the output when I'm using double quotes.

This is my code. I can't change the single quotes because it's the output of another function.

$string = 'New \x26amp\x3Bamp\x3B old';
$string = iconv('ASCII', 'UTF-8//IGNORE', $string);
var_dump($string);

This is my output:

string(26) "New \x26amp\x3Bamp\x3B old"

So how can I change this behavior when using single quotes. Can you convert a string from single quotes to double quotes?

Here's one way to do it:

$str = preg_replace_callback('/\\\x([0-9A-F]{2})/', function ($m) {
    return pack('H*', $m[1]);
}, $str);

It's replacing hex notation with the actual byte it represents. It doesn't have much to do with Unicode.