Solved

php explode function

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to dynamically set the form action using jQuery.

733 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