在PHP 5中将windows-1255转换为UTF-8

时间:2022-09-01 08:58:21

I have a page in my website which gets it's main content from an old mainframe. The content encoding from the mainframe is windows-1255 (Hebrew). My website's encoding is UTF-8.

我在我的网站上有一个页面,它的主要内容来自一个旧的大型机。大型机的内容编码是windows-1255(希伯来语)。我的网站编码是UTF-8。

At first I used an iframe to display the received answer from the mainframe. In that solution I had no problem setting the encoding of the page and the characters display was fine, but I had some problems styling the page responsively (My all website is responsive).

首先,我使用iframe显示从大型机接收到的答案。在这个解决方案中,我在设置页面编码和字符显示方面没有问题,但是我在设计页面响应方面有一些问题(我的所有网站都是响应性的)。

Then I tried fetching the content with file_get_contents and add it in the right place, but all the characters look like this: ����� ��, I then converted the content:

然后我试着抓取内容使用file_get_contents并添加它在正确的地方,但像这样所有的人物:�������,然后我转换的内容:

iconv("cp1255","UTF-8",file_get_contents("my_url"));

The result of that was reversed Hebrew. For example the word "nice" appears as "ecin". The content also includes HTML tags, not only Hebrew text, so I can't simply reverse the text with hebrev.

结果是反过来的希伯来语。例如,单词“nice”以“ecin”出现。内容还包括HTML标签,不仅仅是希伯来文本,所以我不能简单地用hebrev来反转文本。

I saw that in PHP 4 the function fribidi_log2vis exists, which seems to solve my problem, but it's not supported in PHP 5 (I'm working with PHP 5.3.3).

我在PHP 4中看到fribidi_log2vis函数存在,这似乎解决了我的问题,但是PHP 5中不支持这个函数(我使用的是PHP 5.3.3)。

Is there a way handling it better than loading the content into an iframe?

有什么方法比将内容加载到iframe更好?

UPDATE

更新

I tried to fetch a test file that I created (with encoding windows-1255) and my original code works OK. I suspect that the content I'm getting is not windows-1255, at least not in the terms of Hebrew letters order. The conversion on the mainframe might be the cause. I'll have to look into that (I have to wait until Sunday cause I don't have a direct access to the server).

我尝试获取一个我创建的测试文件(使用编码windows-1255),我的原始代码可以正常工作。我怀疑我得到的内容不是windows-1255,至少不是希伯来字母顺序。主机上的转换可能是原因所在。我必须对此进行调查(我必须等到周日,因为我无法直接访问服务器)。

1 个解决方案

#1


2  

The problem that file_get_contents geting the content with ISO 8859-1 as character encoding. You must create a stream context by function stream_context_create with charset Windows-1255 for file_get_contents:

file_get_contents使用ISO 8859-1获取内容作为字符编码的问题。您必须通过函数stream_context_create为file_get_contents创建一个流上下文:

$opts = array('http' => array('header' => 'Accept-Charset: windows-1255,utf-8;q=0.7,*;q=0.7'));
$context = stream_context_create($opts);

$content = file_get_contents('my_url', false, $context);
iconv("cp1255", "UTF-8", $content);

#1


2  

The problem that file_get_contents geting the content with ISO 8859-1 as character encoding. You must create a stream context by function stream_context_create with charset Windows-1255 for file_get_contents:

file_get_contents使用ISO 8859-1获取内容作为字符编码的问题。您必须通过函数stream_context_create为file_get_contents创建一个流上下文:

$opts = array('http' => array('header' => 'Accept-Charset: windows-1255,utf-8;q=0.7,*;q=0.7'));
$context = stream_context_create($opts);

$content = file_get_contents('my_url', false, $context);
iconv("cp1255", "UTF-8", $content);