?
Solved

php explode function

Posted on 2008-10-05
4
Medium Priority
?
622 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

771 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