An error occurred while displaying Korean in PHP - php

Hi all I need your help for my problem.
I try to display text (Korean) from a .txt file but the output is different.
I have a .txt file contains Korean characters like this
냐는 한국을 사랑
but when i try :
$str= file_get_contents($path."result.txt");
echo $str;
on the browser the result came out like this : �먮뒗 �쒓뎅�� �щ옉
but It's OK when i just echo "냐는 한국을 사랑"
IS there something wrong ?
Thank for your help

Either use header("Content-Type: text/html; charset=UTF-8") in your php file or a meta tag in your html <meta charset='utf-8'>. And make sure the font you are using supports unicode characters you need.

Apparently the character encoding of the file is different from the character encoding of the HTML document that your code is generating.
You could dynamically convert the text data in PHP, or you could just use a suitable conversion program to convert the text file. You could just open the text file in a text editor and use Save As to save it as UTF-8 encoded (without BOM), assuming that your PHP is generating a UTF-8 encoded document.

I struggled a while fixing this problem until I discovered this which works perfectly for me:
echo call_user_func_array('mb_convert_encoding', array("행동 방식",'HTML-ENTITIES','UTF-8'));

Related

XML file isn't UTF-8 encoded when created in PHP

I'm trying to output XML file using PHP, and everything is right except that the file that is created isn't UTF-8 encoded, it's ANSI. (I see that when I open the file an do the Save as...).
I was using
$dom = new DOMDocument('1.0', 'UTF-8');
but I figured out that non-english characters don't appear on the output.
I was searching for solution and I tryed first adding
header("Content-Type: application/xml; charset=utf-8");
at the beginning of the php script but it say's:
Extra content at the end of the document
Below is a rendering of the page up to the first error.
I've tryed some other suggestions like not to include 'UTF-8' when creating the document but to write it separately:
$doc->encoding = 'UTF-8'; , but the result was the same.
I used
$doc->save("filename.xml");
to save the file, and I've tryed to change it to
$doc->saveXML();
but the non-english characters didn't appear.
Any ideas?
ANSI is not a real encoding. It's a word that basically means "whatever encoding my Windows computer is configured to use". Getting ANSI is a clear sign of relying on default encoding somewhere.
In order to generate valid UTF-8 output, you have to feed all XML functions with proper UTF-8 input. The most straightforward way to do it is to save your PHP source code as UTF-8 and then just type some non-English letters. If you are reading data from external sources (such as a database) you need to ensure that the complete toolchain makes proper use of encodings.
Whatever, using "Save as" in an undisclosed piece of software is not a reliable way to determine the file encoding.

UTF-8 Encoding not working

I know a number of post is there for utf-8 encoding issue. but i'm getting fail to convert string into utf-8.
I have a string "beløp" in php.
When i print this screen in i frame it printed "bel�p".
After that i tried - utf8_encode("beløp"); - now i got output - "bel�p".
Again i tried iconv("UTF-8", "ISO-8859-1", "beløp"); now i got output - "bel ".
And finally i tried - utf8_encode(utf8_decode("beløp")); now i got output - "bel?p".
Please let me know where i'm wrong and how i can fix it.?
This
bel�p
is an indication that you are outputting a non-UTF-8 character in a UTF-8 context.
Make sure your file is encoded in UTF-8 ( Don't know what editor you're using, but Notepad++/Sublime Text got a "Save with encoding.." option ) and if at the top of your HTML page there's
<meta charset="utf-8">
Hi it's fixed there was problem in my file it was not encoded in "UTF-8".
I fixed by replacing "bel�p" to "beløp".
The reason your conversion does not work is because the original format of your "beløp" text was not in iso-8859-1. The utf8_encode will only work for conversions is from this format. What could work for this type of issues is to use mb_detect_encoding function (http://php.net/manual/en/function.mb-detect-encoding.php) to find out which format the text is originally from, then use the iconv convert from the detected encoding to utf-8. When this is done you have to make sure as mentioned on earlier comments that utf-8 is as encoding in the header.
Note that the php mb detect enconding is not very reliable and can make mistakes on detecting correct encoding. Especially if you do not have a large amount of text. To ensure to display all text correct at all times you need to make sure that all processing at all times is in the same encoding. If you get the text from external sources or web services you should always check the headers for correct encoding before the text is processed.

Php xml encoding issue

Hi guys after 5 hours of research and trying everything I'm so desperate so I write here.
I have an XML file coming from a third party. When I try to parse it with SimpleXMLElement it simply says that the string is not in valid XML format and I also found out that this happens due to ANSI encoding the XML file is having. I tried converting the file to UTF-8 -> it gets read by the parser but all my Cyrillic symbols are lost, replaced by meaningless chars.
Then in notepad++ I copied the content created a file with utf8 encoding and pasted the content -> it was just fine and got read by the parser. I tried to do it with code but no result -> I get contents of the file, create a file with first bytes, the bytes of UTF-8 file, output the content and when I open it -> meaningless chars instead of Cyrillic. Help me please I really need to convert this file to UTF-8 valid for the XML parser or could you please tell me another way to parse the file from XML to array.
Try looking at
http://php.net/manual/en/function.utf8-decode.php
and
http://php.net/manual/en/function.iconv.php
You need to figure out what encoding the original XML file is in, then you can use iconv to convert it to UTF8.

How to deal with inline PHP foreign language characters?

I'm having a problem with my php web app. It is in Portuguese, and while all the data into and from the database inputs and outputs fine, and all the inline html outputs fine, the php strings don't.
For example:
If I have a function that outputs the last update of the database entry:
$output .= 'Última atualização: ' . $date . '.';
When php echoes the above all the accented characters will be garbled. If the same are inline in the HTML or from the database they display normally.
Note: I have declared my encoding as utf-8 both in the html header and in the php script.
The actual PHP file (which is just a text file) also has to have the respective encoding.
And it doesn't work to just add the correct header. You have to open the file, check if the encoding is utf8, if not, cut everything, change the encoding to utf8 and paste everything back. It doesn't work, if you just change the encoding and save the file, unless your text editor has such a function of course!
Try to convert the .php file to UTF-8. This can be done in your editor.
Make sure your browser is set to UTF-8 also. I just ran your above statement:
$output = 'Última atualização: ' . now();
echo $output;
and it looks fine.
If it still doesn't solve your problem, add some more code to get a better idea of what's happening.

Encoding issue with Apache , displaying diamond characters in browser

Request you all to help me set up Apache server on Cent OS. It looks like some encoding issue, but I am not able to resolve it yet.
Instead of HTML content it displays HTML source in (chrome,firefox), IE 9 works fine. It displays � character after each "<" symbol.
http://pdf.gen.in/index1.htm
Second Problem is with PHP. It displays source code of PHP http://pdf.gen.in/index.php with similar diamond characters, wherever it encounters a "<" character. It seems like php issue is related to the first issue.
Those files are encoded with UTF-16LE. For the static HTML page, you might be able to get it to work by setting the charset correctly in the MIME type (it's currently text/html; charset=UTF-8). I don't know how strong PHP's Unicode support is. Try using UTF-8 instead, it's generally more well supported due to its partial overlap with ASCII.
You should use a decent text editor, and always set encoding of php/html to "UTF-8 without BOM".
Create a file named "test.php", paste below codes and save with "UTF-8 without BOM" encoding, then it will work just fine.
<?php
phpinfo();
?>

Categories