<?php

function foobar($match) {
    if (strtolower($match[1][0]) === 'x') {
        $codepoint = hexdec(ltrim(substr($match[1], 1), '0'));
    } else {
        $codepoint = (int)ltrim($match[1], '0');
    }
    return mb_convert_encoding(pack('N', $codepoint), 'UTF-8', 'UTF-32BE');
}
$str = '&#151;';
echo $str;
var_dump( preg_replace_callback('/&#(x[0-9a-f]+|[0-9]+);/i', 'foobar', $str) );