VTS Posted February 6, 2007 Share Posted February 6, 2007 The problem that I am having is finding a way to read data into a database from a textfile that is set up like this: "1 23 23.90 10" Each line begins and ends with the " mark. I have read data from text files before, but they just had the "\n" at the end of the line. What do I do when it has a " mark at the beginning and ending of each line? Thanks in advance, VTS Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 6, 2007 Share Posted February 6, 2007 Do you, or do you not want the " in the data you enter? If you do, you would probably want to do addslashes() on the line before you input, then stripslashes() when you pull it out. If you don't want them in there I'd suggest using some substringing, a regex, or explode(). I myself would use explode('"',$line), but I'm not sure how well it handles the explode token if it's at the beginning and end of the line. In case you can't tell, the first parameter I passed to explode there was single quote, double quote, single quote. Quote Link to comment Share on other sites More sharing options...
VTS Posted February 6, 2007 Author Share Posted February 6, 2007 Well I do not need the " marks entered in my database. Those marks are generated by a program that I do not have access to. I guess I will try using the explode function. thanks for the help, VTS Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 6, 2007 Share Posted February 6, 2007 If you're going to use explode I would suggest this (assuming $line is the line of the file read in): $line ="abcd"; //gibberish! $line .= //read in file (I don't use files often enough to know function call) $line .="dcba"; $linearr = explode('"',$line); $line = $linearr[1]; That should give you the data inside the quotes. Quote Link to comment Share on other sites More sharing options...
VTS Posted February 6, 2007 Author Share Posted February 6, 2007 Thanks, I will try that out now and see how it works. Quote Link to comment Share on other sites More sharing options...
VTS Posted February 6, 2007 Author Share Posted February 6, 2007 Let me add one more thing. Each line has 4 values in it. In this line: "1 23 23.90 10" 1, 23, 23.90, and 10 are all seperate fields so I need to read them in seperately. I tried this: $file = "c:\\data.txt"; if(!file_exists($file)){ echo "File does not exist"; } $handle = fopen($file, "r"); $contents = fread($handle, filesize($file)); fclose($handle); $row = explode("\n", $contents); $rows = count($row); $i = 0; while($i < $rows-1){ $parts = explode('"', $row[$i]); unset($numbers); $numbers = array(); foreach($parts as $part) { $part = trim($part); if(is_numeric($part)) { array_push($numbers, $part); } } $store = array_pop($numbers); $a = array_pop($numbers); $b = array_pop($numbers); $c = array_pop($numbers); echo "</br> $store $a $b $c </br>"; $i++; } but it doesnt seem to work. I am just using an echo statement so I can see that it is working properly before I try inserting any data into the database. I probably just overlooked something stupid like I usually do. Any ideas on why its not working? Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 6, 2007 Share Posted February 6, 2007 What does it output to you exactly? Quote Link to comment Share on other sites More sharing options...
VTS Posted February 6, 2007 Author Share Posted February 6, 2007 nothing at all :-\ Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 6, 2007 Share Posted February 6, 2007 Try using \r\n instead of just \n. Quote Link to comment Share on other sites More sharing options...
VTS Posted February 6, 2007 Author Share Posted February 6, 2007 Still no output....boy I am really starting to feel like a major noob with php now lol. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 6, 2007 Share Posted February 6, 2007 You think you feel bad? How's about the guy who's sitting here being baffled trying to help you? Then again, I'm at work, what else would I be doing? I've got this feeling like $rows contains 0, but I don't know why. Try echoing $rows to see what it prints out. Quote Link to comment Share on other sites More sharing options...
VTS Posted February 6, 2007 Author Share Posted February 6, 2007 $rows has "211" in it. I don't think I have been this confused about something with php in about 4 months now lol. Any other ideas? Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 6, 2007 Share Posted February 6, 2007 Replace your similar lines with this (make sure to backup your stuff first): $i = 0; while($i < $rows-1){ $line = "abcd".$row[$i]."dcba"; $parts = explode('"', $line); unset($numbers); $numbers = array(); $part = trim($parts[1]); if(is_numeric($part)) { array_push($numbers, $part); } See if that works. Quote Link to comment Share on other sites More sharing options...
VTS Posted February 6, 2007 Author Share Posted February 6, 2007 I just found out what it is doing....instead of having one field in $part, it has the whole line. I guess I need to go a little further with my exploding now and use it to seperate the fields. When I used echo on $part right after I used trim() on it, I get a whole line in it. I am about to go and try this now and see how it works. Quote Link to comment Share on other sites More sharing options...
VTS Posted February 6, 2007 Author Share Posted February 6, 2007 Ok, I figured it out . Here is what I used: set_time_limit(999); $file = "c:\\data.txt"; if(!file_exists($file)){ echo "File does not exist"; } $handle = fopen($file, "r"); $contents = fread($handle, filesize($file)); fclose($handle); $row = explode("\r", $contents); $rows = count($row); $i = 0; while($i < $rows-1){ $parts = explode('"', $row[$i]); unset($numbers); $numbers = array(); foreach($parts as $part) { $part = trim($part); echo "</br>trimed part = $part"; $x = 0; $partA = explode(" ", $part); //new lines added here to store each field seperately foreach($partA as $var){ if(is_numeric($var)) { echo "</br> *var = $var"; array_push($numbers, $var); } $x++; } } $store = array_pop($numbers); $a = array_pop($numbers); $b = array_pop($numbers); $c = array_pop($numbers); echo "</br> store = $store || a= $a || b= $b || c= $c </br>"; $i++; } Thanks for the help Balmung! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.