Click here to Skip to main content
15,436,489 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a scenario as described below.

File "a" Server.php:

Reads a report name out of a .txt file and echo's it.

File "b" Client.html:

Send a request to Server.php to read the file and receives the name of the report(XmlHttpRequest.responsetext)

This is working well for all types of reports and foreign languages when i just echo it, however now i want to encode the report name in Server.php and then send it to Client.html. And the encoding is failing or not giving me the text i want.

For Example:
The actual report name is:
Type [11432304]español Date10-26-2011-Time10-52-11
After encoding else where in the project it is: 
Type+%5B11432304%5Despa%F1ol+Date10-26-2011-Time10-52-11
But when i encode it is:
%EF%BB%BFType+%5B11432304%5Despa%C3%B1ol+Date10-26-2011-Time10-52-11


PHP
Server.php
header( 'Content-Type: text/html; charset=utf-8' );
if(isset($_GET['read'])
{
        $newfileNameandType  = "Sample.txt"; //This is a notepad saved with [11432304]español Date10-26-2011-Time10-52-11 in utf-8 format.
        if (file_exists($newfileNameandType)) 
	{
		$myfile=fopen($newfileNameandType,'r');
                $NewReportAddress = fread($myfile, filesize($newfileNameandType));
		fclose($myfile);
                echo urlencode($NewReportAddress); // This is not giving me the correct encoded text like elsewhere.
        }
}
?>


Code gurus am i missing something? How do i get "Type+%5B11432304%5Despa%F1ol+Date10-26-2011-Time10-52-11" when i encode but not "%EF%BB%BFType+%5B11432304%5Despa%C3%B1ol+Date10-26-2011-Time10-52-11".

Thanks for your time.
Posted

Some text editors, including Notepad, add byte order mark (BOM) to indicate UTF-8 text.
It's the EF BB BF at the start of your text file.
See http://en.wikipedia.org/wiki/Byte_order_mark

My suggestion is add a code to detect and remove the BOM after reading your text files or before using any text string.
You should also ditch Notepad and use a better text editor.

Cheers.
 
Share this answer
 
Yes, PHP sucks some times.
Use "rawurlencode" instead of "urlencode" (see here for more info[^]).
I hate it when the documentation says "for historical reasons" - the best is the parameter-list of "implode"[^] ;)

Sorry, I have misread your question...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900