The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 16, 2019, 05:47:27 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: exploding empty string gives junk character  (Read 2364 times)
nattsurfaren
Journeyman
***
Offline Offline

Posts: 64


View Profile
« on: October 05, 2008, 11:03:30 AM »


Examine this code
Code:
                    $doneaccountsarr = array();
    if (file_exists("doneaccounts.txt"))
    {
        $doneaccounts = file_get_contents("doneaccounts.txt");
        $doneaccounts = str_replace("\r", "", $doneaccounts);
        $doneaccountsarr = explode("\n", $doneaccounts);
       
    }
If file doneaccounts exist but is empty it will result in $doneaccountsarr array containing a newline.

To fix this use an empty check.

Code:
                   $doneaccountsarr = array();
    if (file_exists("doneaccounts.txt"))
    {
        $doneaccounts = file_get_contents("doneaccounts.txt");
        if(!empty($doneaccounts))
        {
        $doneaccounts = str_replace("\r", "", $doneaccounts);
        $doneaccountsarr = explode("\n", $doneaccounts);
        }
    }
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: October 06, 2008, 08:55:43 AM »

Natt - you're saying that an empty file (completely empty, like you just TOUCHed a file name) will explode out as an array with a single newline?

I have to try - that'd be an interesting bug if reliable.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #2 on: October 06, 2008, 09:09:21 AM »

Interesting Natt. You're right, even a zero-length file will return a single blank entry in the array.

Looking back at my code, it seems that I've avoided this naturally, because the way I'll typically pull a file in looks like this:

Code:
<?php

$fname 
'./testfile.txt';
$arr = array();
if (
file_exists($fname))
{
$buff str_replace(chr(13), ''trim(file_get_contents($fname)));
if (strlen($buff)) $arr explode(chr(10), $buff);
}

?>


Note the trim and evaluating the strlen AFTER I ditch chr(13)s - this will only explode if there's forreals something in the buffer.

Thanks mang, nice to know.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #3 on: October 06, 2008, 09:12:44 AM »

I actually just ran into this yesterday coincidentally. I have a ghetto version of an email blaster that runs from a file of emails. I pop a few emails off the end and resave the file. at the end however, even though there are no characters in the file, it is 0b, the array length is 1 when i explode on \r\n

So i dealt with it by double checking the length of the first element of the array, which should be greater than 0.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!