title: PHP string convert encoding from utf8 to cp1251 with replace impossible characters via html-mnemonics
body: <p>I convert string encoding from wide <code>utf8</code> to limited encoding <code>cp1251</code>. I need to preserve some characters not included into cp1251.</p>
  
  <p>In python 2.x there is a special function, which during encoding conversion replaces impossible characters with html-entities:</p>
  
  <pre><code># -*- coding: utf-8 -*-
  
  s_in = "Ø 125 mm".decode('utf8')
  s_out = s_in.encode('cp1251', 'xmlcharrefreplace')
  print s_out # prints &amp;#216; 125 mm
  </code></pre>
  
  <p><a href="http://ideone.com/RPXeEB" rel="nofollow noreferrer">Live example on ideone</a></p>
  
  <p>Is there any ready-to-use func/lib in PHP to do the task?</p>
  
  <p>My code is:</p>
  
  <pre><code>&lt;?php
  $in = 'Ø 125 mm';
  $out = mb_convert_encoding($in, 'cp1251', 'utf8');
  echo $out; // prints ? 125 mm
  </code></pre>
  
  <p><a href="http://sandbox.onlinephpfunctions.com/code/ef84c790a08c2de3c4ef87ef18a1a8d75c960791" rel="nofollow noreferrer">Live example on sandbox.onlinephpfunctions</a></p>