Solved

php explode function

Posted on 2008-10-05
4
600 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
Comment Utility
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
Comment Utility
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
Comment Utility
did you try

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

Let me know if that works.
0
 
LVL 1

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now