title: Decoding numeric html entities via PHP
body: <p>I have this code to decode numeric html entities to the UTF8 equivalent character.</p>
  
  <p>I'm trying to convert this character: </p>
  
  <blockquote>
    <p>&amp;#146;</p>
  </blockquote>
  
  <p>which should output: </p>
  
  <blockquote>
    <p>&#146;</p>
  </blockquote>
  
  <p>However, it just disappears (no output). (i've checked the source code of the page, the page has the correct utf8 character set headers/meta tags).</p>
  
  <p>Does anyone know what is wrong with the code? </p>
  
  <pre><code>function entity_decode($string, $quote_style = ENT_COMPAT, $charset = "UTF-8") {    
       $string = html_entity_decode($string, $quote_style, $charset);
  
       $string = preg_replace_callback('~&amp;#x([0-9a-fA-F]+);~i', "chr_utf8_callback", $string);
       $string = preg_replace('~&amp;#([0-9]+);~e', 'chr_utf8("\\1")', $string);
  
      //this is another method, which also doesn't work.. 
       //$string = preg_replace_callback("/(\&amp;#[0-9]+;)/", "entity_decode_callback", $string);
  
       return $string; 
  }
  
  
  
  
  function chr_utf8_callback($matches) { 
       return chr_utf8(hexdec($matches[1])); 
  }
  
  function chr_utf8($num) {   
       if ($num &lt; 128) return chr($num);
       if ($num &lt; 2048) return chr(($num &gt;&gt; 6) + 192) . chr(($num &amp; 63) + 128);
       if ($num &lt; 65536) return chr(($num &gt;&gt; 12) + 224) . chr((($num &gt;&gt; 6) &amp; 63) + 128) . chr(($num &amp; 63) + 128);
       if ($num &lt; 2097152) return chr(($num &gt;&gt; 18) + 240) . chr((($num &gt;&gt; 12) &amp; 63) + 128) . chr((($num &gt;&gt; 6) &amp; 63) + 128) . chr(($num &amp; 63) + 128);
       return '';
  }
  
  function entity_decode_callback($m) { 
       return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); 
  } 
  
   echo '=' . entity_decode('&amp;#146;');
  </code></pre>