Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im trying to explode a text file using php. First, I used file to get the entire text file, and then exploded line by line using explode, but when I run the code I get the follow notice in the list line:

Notice: Undefined offset: 6 in

However, the code gives me the result I want but I also get a bunch of notices .Please can anyone help me out

PHP
foreach(file("singles.txt") as $singlesFile)
{
list($name,$gender,$age,$personality,$os,$min,$max)=explode(",",$singlesFile);
if($name==$userName)
{
$persongender = $gender;
$personage = $age;
$personpersonality = $personality;
$personos = $os;
$personmin = $min;
$personmax = $max;
}
Posted

1 solution

check the count of element like below

PHP
$segments = explode(",",$singlesFile);
if (count($segments) !== 7) {
    // error
} else {
    list($name,$gender,$age,$personality,$os,$min,$max)= $segments;
}
 
Share this answer
 
v2
Comments
Alexander24 22-Apr-14 23:36pm    
I found the solution,it happens because we I add a new line to my text file, it skips one line.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900