fork download
  1. <?php
  2.  
  3. function foobar($match) {
  4. if (strtolower($match[1][0]) === 'x') {
  5. $codepoint = hexdec(ltrim(substr($match[1], 1), '0'));
  6. } else {
  7. $codepoint = (int)ltrim($match[1], '0');
  8. }
  9. echo $codepoint;
  10. return mb_convert_encoding(pack('N', $codepoint), 'UTF-8', 'UTF-16BE');
  11. }
  12. $str = '&#151;';
  13. echo $str;
  14. var_dump( preg_replace_callback('/&#(x[0-9a-f]+|[0-9]+);/i', 'foobar', $str) );
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
&#151;151string(3) "—"