[PHP] .DAT file with PHP
I want to use PHP to show the contents of the DAT from back to front. HOw do I do that? Thanks!
RE: [PHP] .DAT file with PHP
I used the following code that Paul suggested, but it didn't reverse my content. The file I would want to have the content reversed is as attached. Chinese characters is in the file so... Suggestions? -Original Message- From: Paul Novitski [mailto:[EMAIL PROTECTED] Sent: Thursday, March 09, 2006 2:58 AM To: php-general@lists.php.net Subject: Re: [PHP] .DAT file with PHP At 10:27 AM 3/8/2006, Rory Browne wrote: >$filename = "filename.txt"; >$file_content = join("\n", array_reverse(file($filename))); echo >$file_content; Rory, I think you've got the logic right. Tangentially, however, I recommend that you break it out into separate statements and not throw multiple functions into the same statement -- it's hard to proofread, it's hard to pinpoint where errors occur, and it's next to impossible to insert echo statements to debug the process. Also for ease of debugging & maintenance, I recommend indicating the type of each variable with a prefix (a=array, s=string, etc.): $sFilename = "filename.txt"; $aFile_content = file($sFilename); $aFile_reverse = array_reverse($aFile_content); $sDisplay_content = join("\n", $aFile_reverse); echo $sDisplay_content; I don't think PHP will "care" whether it's broken out or not -- internally it's having to create temporary variables on the fly to store incremental values -- but your future self and other folks reading your code will thank you for it. Regards, Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] .DAT file with PHP
Sorry im new but, how do we read from a file to an array? I've studied C but not with PHP and it's not working for me... Suggestions? -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 08, 2006 9:58 PM To: [EMAIL PROTECTED]; php-general@lists.php.net Subject: RE: [PHP] .DAT file with PHP [snip] I want to use PHP to show the contents of the DAT from back to front. HOw do I do that? [/snip] Open the file, read it into an array, read the array backwards and close the file. Start with http://www.php.net/fopen and http://www.php.net/array they are both in the manual. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php smime.p7s Description: S/MIME cryptographic signature
RE: [PHP] .DAT file with PHP
I use: $frob = fopen('data/rob.dat','r'); if ($frob) { while (!feof($frob)) { $buffer = fgets($frob, 4096); echo $buffer; $glorp[] = $buffer; // places line in array } } $read = array_reverse($glorp); However, only "Array" text came out when I do an echo $read What's wrong...? Thanks for your patience! -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 08, 2006 11:50 PM To: [EMAIL PROTECTED]; [EMAIL PROTECTED]; php-general@lists.php.net Subject: RE: [PHP] .DAT file with PHP [snip] Sorry im new but, how do we read from a file to an array? I've studied C but not with PHP and it's not working for me... Suggestions? [/snip] First, read and understand, as much as possible, TFM. It should go something like this $foo = fopen("my.dat", "r"); while(!feof($foo)){ $bar = fgets($foo, 4096); // gets a line hopefully $glorp[] = $bar; // places line in array } Once you have done this look at http://www.php.net/array for all of the things that you can do with the $glorp array y smime.p7s Description: S/MIME cryptographic signature