Solved

php explode function

Posted on 2008-10-05
4
616 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 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

690 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