PHP string convert encoding from utf8 to cp1251 with replace impossible characters via html-mnemonics

2.8k Views Asked by At

I convert string encoding from wide utf8 to limited encoding cp1251. I need to preserve some characters not included into cp1251.

In python 2.x there is a special function, which during encoding conversion replaces impossible characters with html-entities:

# -*- coding: utf-8 -*-

s_in = "Ø 125 mm".decode('utf8')
s_out = s_in.encode('cp1251', 'xmlcharrefreplace')
print s_out # prints Ø 125 mm

Live example on ideone

Is there any ready-to-use func/lib in PHP to do the task?

My code is:

<?php
$in = 'Ø 125 mm';
$out = mb_convert_encoding($in, 'cp1251', 'utf8');
echo $out; // prints ? 125 mm

Live example on sandbox.onlinephpfunctions

2

There are 2 best solutions below

2
Syed Aqeel On

By using iconv() function in PHP you can convert string from one to another encoded scheme. Example:

$out = iconv("UTF-8", "CP1251//IGNORE", $in);

If you append the string //IGNORE, characters that cannot be represented in the target charset are silently discarded

For complete description see link: http://php.net/manual/en/function.iconv.php

1
ASR On

Try json_encode with JSON_UNESCAPED_UNICODE

$in = 'Ø 125 mm';
$out = json_encode($in, JSON_UNESCAPED_UNICODE);
echo json_decode($out, true);    

http://sandbox.onlinephpfunctions.com/code/cfd9f38ed7ad8b668285be31004bfe2578da6436