Solved

php explode function

Posted on 2008-10-05
4
611 Views
Last Modified: 2013-12-12
I am using the explode function to extract data from a csv file.

I have done this MANY times before.

Here are the first few lines of code:
$open = fopen($thefile, "r");
      if($open) {
            $data = file($thefile);
            for ($n = 1; $n < count($data); $n++) {
                   $gl = explode(",", $data[$n]);

An example of the actual data with the problem is like this:
mje,201,John,Smith,A1234,

Notice the last field is blank (or null).

If I do a test like if($gl[5] == "") it fails (it is NOT null).

As a part of debugging this I looked at the length of the field. I discovered that if $gl[5] is null, the length is 2. If it is not null (like for example, the value 'abc'), then the length is 2 plus the real length. The length of abc is 5.

What's going on here? Why can't I test if $gl[5] == "" and have it work?

Why are the lengths 2 longer that the "real" length. Are there hidden characters or ??

I think I just saw it. There is a CR/LF at the end of each line in the csv file & that is the 2 extras.

Is that correct?

Do I have to strip off the last two characters from the last item in the row before I store it in the database (else a subsequent test for equal may fail?)      
0
Comment
Question by:Richard Korts
  • 2
4 Comments
 
LVL 6

Expert Comment

by:trickyidiot
ID: 22646758
use the function empty (http://www.php.net/empty)

if (empty($g[5])){
  // do whatever
}
0
 
LVL 3

Accepted Solution

by:
wktang83 earned 500 total points
ID: 22647661
empty($gl[5]) will still return false, since $gl[5] has a special end of line character there.

I would suggest try this instead:

if (trim($gl[5] == "") {

}

Please let me know if this works.
0
 
LVL 1

Expert Comment

by:jamespbyrne
ID: 22651031
did you try

if ($g[5]==null){
//do your processing here
}

Let me know if that works.
0
 
LVL 1

Expert Comment

by:jamespbyrne
ID: 22684126
did any of these solutions work? could you please let us know.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question