将字符串从 UTF-8 解码为 Windows1256

Decoding String from UTF-8 to Windows1256

我有下面的代码,它试图将字符串从 UTF 转换为 CP1256。我想把字符串解码成阿拉伯语,页面加密固定为UTF8

<?php 

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

echo $string;

?> 
$strings = "ãÍãÏ Úæäí ãÍãæÏ Úáí";

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

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

echo var_dump($convert);

所以您的阿拉伯语文本已被编码为 Windows-1256,然后被错误地编码为 Windows-1252。

如果您的源文件是 UTF-8 编码的,答案是:

<?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;

?>

如果您的源文件是 "windows-1252" 编码的,那么您必须使用:

<?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;

?>

如果您 $string 实际上来自数据库或文件,那么您必须在应用任何转换之前确定源的编码。