Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting windows-1255 to UTF-8 in PHP 5

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.

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).

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:

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.

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).

Is there a way handling it better than loading the content into an 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).

like image 614
Itay Gal Avatar asked Jan 03 '14 15:01

Itay Gal


1 Answers

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:

$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);
like image 190
Dmitriy.Net Avatar answered Sep 18 '22 15:09

Dmitriy.Net