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

时间:2016-05-24 07:27:04

标签: php utf-8 utf

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

<?php 

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

echo $string;

?> 

2 个答案:

答案 0 :(得分:2)

因此,您的阿拉伯语文本已在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实际来自数据库或文件,则必须在应用任何转换之前确定源代码的编码。

答案 1 :(得分:0)

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

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

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

echo var_dump($convert);