-1

I have the code below, which tries to convert a string from UTF to CP1256. I want to decode the string to arabic, and the page encryption is fixed to UTF8

<?php 

$string = "ãÍãÏ Úæäí ãÍãæÏ Úáí";
$string = iconv("UTF-8//TRANSLIT//IGNORE", "Windows-1252//TRANSLIT//IGNORE",     $string);

echo $string;

?> 

2 Answers2

2

So your Arabic text, has been encoded in Windows-1256 and then incorrectly encoded to Windows-1252.

If your source file is UTF-8 encoded, the answer is:

<?php

$string = "ãÍãÏ Úæäí ãÍãæÏ Úáí";
$string = iconv("UTF-8//TRANSLIT//IGNORE", "Windows-1252//TRANSLIT//IGNORE", $string);
# $string is now back to its 1256 encoding. Encode to UTF-8 for web page
$string = iconv("Windows-1256//TRANSLIT//IGNORE", "UTF-8//TRANSLIT//IGNORE", $string);

echo $string;

?>

If your source file is "windows-1252" encoded, then you must use:

<?php

$string = "ãÍãÏ Úæäí ãÍãæÏ Úáí";
# Interperate windows-1252 string as if it were windows-1256. Encode to UTF-8 for web page
$string = iconv("Windows-1256//TRANSLIT//IGNORE", "UTF-8//TRANSLIT//IGNORE", $string);

echo $string;

?>

If you $string actually comes from a database or file, then you have to determine the encoding of the source before applying any conversion.

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
  • Than you very much, you're code worked like a charm for few minutes, then somehow it stopped working, the real example I am working on can be found in this question [link](http://stackoverflow.com/questions/37405245/arabic-pdf-form-to-php-to-mysql-unicode-decoding) and if you want a copy of the PDF file, I can upload it. – Mohammad Awni Ali May 24 '16 at 15:18
0
$strings = "ãÍãÏ Úæäí ãÍãæÏ Úáí";

setlocale(LC_CTYPE, 'nl_NL.UTF-8');

$convert = iconv('UTF-8', 'windows-1251//TRANSLIT//IGNORE', $strings);

echo var_dump($convert);
Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
sasikaran
  • 142
  • 5