Page 1 of 1

[solved - possibly] htmlentitising odd behaviour

Posted: Fri May 27, 2005 5:35 pm
by leenoble_uk
I've got a big textarea field which my client will use to create plain text files on the server but it seems to be having a problem with the UK pound sign (£).

I don't really want to htmlentities it to put it in the txt file, I just want to write the £ sign. The odd thing is that no matter how I approach it I end up with an odd character before the £.

Taking a text file containing the offending character and presenting it to be edited I have obviously htmlentitised the whole text file and the £ sign shows up fine and is encoded as £

When I submit the form and just echo out the $_POST["textfield"] it displays as £ which is what I want to write to file.

but when I pass it to the function:

Code: Select all

function upload_text($directoryPath, $textFile, $text)
{
	//echo "We're going to be obliterating ".$directoryPath."/".$textFile;
	
	
	$fp = @fopen($directoryPath."/".$textFile,"w");
	ftruncate($fp,0);
	fwrite($fp, $text);	
}
the resultant text file has the 'not sign' ¬ (not encoded) in front of it.

If however I htmlentitise the text before writing to file I end up with £ instead. There is nothing odd about the space before the pound sign so I assume it is something to do with text encoding. What can I do to fix this?
Cheers
Lee

edit was to add solved-ish to subject

Posted: Sat May 28, 2005 4:33 am
by JAM
I'm not helping here I guess, but I might have misunderstood something (?):

Code: Select all

<form method="post">
 <textarea name="textfield">Something with £2.00</textarea>
 <input type="submit" />
</form>
<?php
    print_r($_POST['textfield']);
    upload_text('test.txt', $_POST['textfield']);
    function upload_text($textFile, $text) {
        $fp = fopen($textFile,"w");
        ftruncate($fp,0);
        fwrite($fp, $text);    
    }
?>
Result:

Code: Select all

Something with £2.00
I get the £ as-is, without issues. I'm not using headers or anything else (DOCTYPE HTML settings or similiar) tho...

Posted: Sun May 29, 2005 9:42 am
by wwwapu
I don't understand the problem either but something can be done

Code: Select all

function upload_text($directoryPath, $textFile, $text)
{
    //echo "We're going to be obliterating ".$directoryPath."/".$textFile;
    $fp = @fopen($directoryPath."/".$textFile,"w");
    fwrite($fp, $text);
    fclose($fp);
}
you don't need to truncate(), because
PHP manual wrote:'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

[Solved - possibly] htmlentitising odd behaviour

Posted: Tue May 31, 2005 6:45 am
by leenoble_uk
JAM touched on the solution with the last line of his reply. I changed this

Code: Select all

<!DOCTYPE html PUBLIC &quote;-//W3C//DTD XHTML 1.0 Transitional//EN&quote;
        &quote;http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd&quote;>
<html xmlns=&quote;http://www.w3.org/1999/xhtml&quote; xml:lang=&quote;en&quote; lang=&quote;en&quote;>
<head>
	<meta http-equiv=&quote;content-type&quote; content=&quote;text/html; charset=utf-8&quote; />
to

Code: Select all

<html>
<head>
and the problem disappeared. I'm guessing it's the charset meta tag which is causing the issue although for the life of me I can't work out how.

When I echo out the $_POST array the £ sign appears correctly. This is obviously echoed out prior to the doctype declaration. The text is written to file, again before the doctype declaration yet is somehow affected by it.

I'll admit I don't understand why I would choose one charset setting over another and have no idea how changing it might affect other browsers, I just go with the default setting in BBEdit. I'll have a play around now to find a charset encoding which works so that I can keep the site as XHTML compliant.

Thanks for pointing me in the right direction no matter how unintentional or not it may have been.