PHP Generate Ascii file? - php

I am generating a CSV file but the people who are processing these file tells me it needs to be in ASCII format?? How do I go about to make that?
This is what I have to generate the file:
$filename = '/logs/'.date('Ymd').'.txt';
$myfile = fopen($filename,'a');
fwrite($myfile, $data);
fclose($myfile);
This file generates fine and opens fine...everything is ok to the naked eye but they said it needs to be in ascii format...
Output of file:
"","932-4","Mike","Tanner","","1234 Testing Lane","","Los Angeles","CA","90066","","(993)857-7727","","","","SALE","","","V","4111111111111111","01/14","AXLW","","ZENC","","","REG","","511.80","","07/21/11","932-359","D1234","4","","1","","","","","","","Tanner","Mike","","1234 Testing Lane","","CA","Los Angeles","90066","","CC","","","","Y","100.00","","100.00","","","","","","","","Y","11.8","info#info.com","359","001","001","(993)857-7727","(993)857-7727","","","","","","","","","","","","","","","","","","","","222","","","","","","","","","","","","","",
Anyone?
Thanks...

I'm going to play Carnac the Magnificent and say that you're just using a line-feed (ascii 10, aka \n) to terminate each line. I'll bet they want carriage-return plus line-feed (ascii 13,10). Just a wild guess. :)

ANSI = Windows-1252, so probably: $data = iconv("windows-1252","ASCII",$data);

Related

PHP - writing special characters with fwrite- looks good in notepad but wrong in browser

I have a small issue with writing special characters (Danish, but also some symbols) text to .txt-files with PHP.
Take this example:
<?php
$words = "å æ ø";
$file = fopen("test.txt","w");
fwrite($file, ($words));
fclose($file);
?>
Above code will work fine and looks correct when I open it in notepad. But for a online purpose I need to open the generated .txt-file with a browser (fx. Firefox) and in the browser the characters are not shown correctly unless I chose "Unicode" as "character encoding" from the show-menu in Firefox, default character encoding in Firefox is "western".
If I make a normal .txt-file with notepad and write "å æ ø" and saving it the normal way it looks correctly in a browser.
I have been searching around looking for information about encoding options for the fwrite but I don't really know where to start.
Kind Regards
The solution is the following: As the UTF-8 byte-order mark is '\xef\xbb\xbf' we should add it to the document's header.
<?php
function writeStringToFile($file, $string) {
$f = fopen($file, "wb");
$string= "\xEF\xBB\xBF".$string; // utf8 bom
fputs($f, $string);
fclose($f);
}
?>
read about BOM

fopen 't' compatibility mode in php

It is said, that fopen can use t mode to convert \n to \r\n. So, questions:
1) How should i use t mode when i need to read and write (r+)? Should it be r+t or rt+ or tr+? Same question for b, should i write r+b or how?
2) I've tried all variants on debian linux to convert file, that contains only \n to \r\n using magic mode t (wanna understand how it works). But it does not work. What am I doing wrong? When t mode works?
Here is my code:
// Write string with \n symbols
$h = fopen('test.file', 'wt');
fwrite($h, "test \ntest \ntest \n"); // I've checked, after file is being created
fclose($h); // \n symbols are not substituted to \r\n
// Open file, that contains rows only with \n symbols
$h = fopen('test.file', 'rt');
$data = fread($h, filesize('test.file'));
fclose($h);
// I want to see what's inside
$data = str_replace("\n", '[n]', $data);
$data = str_replace("\r", '[r]', $data);
// finally i have only \n symbols, \r symbols are not added
var_dump($data);
From: http://php.net/fopen
Windows offers a text-mode translation flag ('t') which will transparently translate \n to \r\n when working with the file. In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.
So no Linux. Also, according to the spec r+t or r+b would be correct (but only on Windows).

PHP echo the equivalent of CR LF of Notepad

I need to export some data using PHP and for each line I'm adding a \r\n. When I open the exported data file that I downloaded, I see that the \r\n is interpreted as [LF] in Notepad.
But the application in which I open the file doesn't read the [LF] as a new line.
Now if I do a [CR][LF] in Notepad, the application can read the [CR][LF] as a new line.
How can I echo the equivalent of [CR][LF] with PHP?
It's as simple as doing:
echo "\r\n";
(note the double quotes)
Do echo PHP_EOL;
That way it'll always display the correct linefeed/carriage return combination that's valid for the system you're on, since not all OSes use the same newline convention. (More information: PHP global constants.)
Problem solved: the string was passed in the POST parameter. Removing the \r.
You just have to do a str_replace("\n", "\r\n", $...);.

Can I edit a binary (hex) file with PHP, replace a string and save without corrupting the binary?

I have a little problem :)
I have a binary file -executable- which I want to edit and replace a
string
I open the file with PHP, perform a replace and save it
Saved binary is corrupt.
If I do it using SED or any hexadecimal editor, it works fine.
Can I open, edit (replace a string) and save a binary file using PHP?
Thank you very much!
On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in fopen() mode parameter. Then use fread and fwrite on the file.
<?php
$fp = fopen('binary_file.bin', 'w+b');
fwrite($fp, '1');
fclose($fp);
?>
You could read the file byte by byte, doing a binary comparation, and a byte to byte replacement. Write PHP code that look like C code :D

Search And Replace Special Characters PHP

I am trying to search and replace special characters in strings that I am parsing from a csv file. When I open the text file with vim it shows me the character is <95> . I can't for the life of me figure out what character this is to use preg_replace with. Any help would be appreciated.
Thanks,
Chris Edwards
0x95 is probably supposed to represent the character U+2022 Bullet (•), encoded in Windows code page 1252. You can get rid of it in a byte string using:
$line= str_replace("\x95", '', $line);
or you can use iconv to convert the character set of the data from cp1252 to utf8 (or whatever other encoding you want), if you've got a CSV parser that can read non-ASCII characters reliably. Otherwise, you probably want to remove all non-ASCII characters, eg with:
$line= preg_replace("/[\x80-\xFF]/", '', $line);
If your CSV parser is fgetcsv() you've got problems. Theoretically you should be able to do this as a preprocessing step on a string before passing it to str_getcsv() (PHP 5.3) instead. Unfortunately this also means you have to read the file and split it row-by-row yourself, and this is not trivial to do given that quoted CSV values may contain newlines. By the time you've written the code to handle properly that you've pretty much written a CSV parser. So what you actually have to do is read the file into a string, do your pre-processing changes, write it back out to a temporary file, and have fgetcsv() read that.
The alternative would be to post-process each string returned by fgetcsv() individually. But that's also unpredictable, because PHP mangles the input by decoding it using the system default encoding instead of just giving you the damned bytes. And the default encoding outside of Windows is usually UTF-8, which won't read a 0x95 byte on its own as that'd be an invalid byte sequence. And whilst you could try to work around that using setlocale() to change the system default encoding, that is pretty bad practice which won't play nicely with any other apps you've got running that depend on system locale.
In summary, PHP's built-in CSV parsing stuff is pretty crap.
Following Bobince's suggestion, the following worked for me:
analyse_file() -> http://www.php.net/manual/en/function.fgetcsv.php#101238
function file_get_contents_utf8($fn) {
$content = file_get_contents($fn);
return mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}
if( !($_FILES['file']['error'] == 4) ) {
foreach($_FILES as $file) {
$n = $file['name'];
$s = $file['size'];
$filename = $file['tmp_name'];
ini_set('auto_detect_line_endings',TRUE); // in case Mac csv
// dealing with fgetcsv() special chars
// read the file into a string, do your pre-processing changes
// write it back out to a temporary file, and have fgetcsv() read that.
$file = file_get_contents_utf8($filename);
$tempFile = tempnam(sys_get_temp_dir(), '');
$handle = fopen($tempFile, "w+");
fwrite($handle,$file);
fseek($handle, 0);
$filename = $tempFile;
// END -- dealing with fgetcsv() special chars
$Array = analyse_file($filename, 10);
$csvDelim = $Array['delimiter']['value'];
while (($data = fgetcsv($handle, 1000, $csvDelim)) !== FALSE) {
// process the csv file
}
} // end foreach
}

Categories