jschofield Posted March 20, 2007 Share Posted March 20, 2007 Hello fellow phpers, I am having some trouble with this script and good ol boss needs it done by tomorrow so please HELP anyone! Ok What I am trying to do is split the last name and first name that is in one field into two fields and having a ton of trouble. I figure to use explode but I must be doing something wrong. I have posted my script and data below. Please HELP..I would greatly be thankful. Thanks everyone. Johnnie DATA: 0000190147,ALEXANDER PAUL,7247522388 ,EN,1,02/28/2007, 0000170212,ALTENBURG SETH,7247584035 ,EN,3,02/28/2007, 0000200161,ALTERS PAIGE,7247581035 ,EN,0,02/28/2007, 0000170141,ALTMAN CHASE,7246744285 ,EN,2,02/28/2007, Script: ?php $inputfile = "C:/SMDIM/Data/schoolmessenger.txt"; $outputfile = "C:/SMDIM/Data/RIVERSIDE_BEAVER_CO_SD_ALL_STU.CSV"; $logfilename = "C:/SMDIM/Logs/RIVERSIDE_BEAVER_CO_SD_ALL_STU_LOG.TXT"; include'C:/SMDIM/Translators/importlibrary.inc.php'; if(!$in = fopen($inputfile, "r")) { wlogdie("Failed to open $inputfile for reading!"); }else{ wlog("Opened $inputfile for reading."); } if(!$out = fopen($outputfile, "w")) { wlogdie("Failed to open $outputfile for writing!"); }else{ wlog("Opened $outputfile for writing."); } while ($line = fgetcsv($in, 1000, ",")) { $trimmedphone = str_replace(" ", "", $line[2]); $formattedlang = str_replace("EN", "ENGLISH", $line[3]); $tmpline = '"' . $line[0]; $tmpline .= '"' . ',' . '"' . $line[1]; $tmpline .= '"' . ',' . '"' . $trimmedphone; $tmpline .= '"' . ',' . '"' . $formattedlang; $tmpline .= '"' . ',' . '"' . $line[4]; $tmpline .= '"' . ',' . '"' . "Student" . '"'; $tmpline .= "\r\n"; $count++; fwrite($out, $tmpline); } if($count !== '0') { wlog("Finished the import with a total of $count records"); }else{ wlog("Failed to import data!"); } //Close the input and output files. fclose($in); echo "Closed the input file." . "\r\n"; fclose($out); echo "Closed the ouput file." . "\r\n"; ?> Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/ Share on other sites More sharing options...
genericnumber1 Posted March 20, 2007 Share Posted March 20, 2007 something like list($firstname, $lastname) = explode(' ', $line[1]); though this is problematic for people with multiple names Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211479 Share on other sites More sharing options...
effigy Posted March 20, 2007 Share Posted March 20, 2007 Assuming there is only one space... <pre> <?php $lines = array( '0000190147,ALEXANDER PAUL,7247522388 ,EN,1,02/28/2007,', '0000170212,ALTENBURG SETH,7247584035 ,EN,3,02/28/2007,', '0000200161,ALTERS PAIGE,7247581035 ,EN,0,02/28/2007,', '0000170141,ALTMAN CHASE,7246744285 ,EN,2,02/28/2007,' ); foreach ($lines as $line) { $fields = explode(',', $line); $names = explode(' ', $fields[1]); array_splice($fields, 1, 1, $names); print_r($fields); } ?> </pre> Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211489 Share on other sites More sharing options...
Barand Posted March 20, 2007 Share Posted March 20, 2007 try <?php $lines = file('iinput.txt'); $fp = fopen ('newfile.txt', 'w'); foreach ($lines as $line) { list ($a, $b, $c, $d, $e, $f) = explode(',', $line); list ($b1, $b2) = explode(' ', $b); fwrite ($fp, sprintf('%s,%s,%s,%s,%s,%s,%s,%s', $a, $b1, $b2, $c, $d, $e, $f, "\r\n")); } fclose ($fp); ?> Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211491 Share on other sites More sharing options...
jschofield Posted March 20, 2007 Author Share Posted March 20, 2007 Hello all and thanks for the replys. I couldnt get the first two to work but the one with Barand came close but all of my data is 0's. Im going to keep trying a few other things. Thank you to all that helped! Johnnie Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211504 Share on other sites More sharing options...
per1os Posted March 20, 2007 Share Posted March 20, 2007 heck I would just do this, (borrowing Barands code) <?php $lines = file('iinput.txt'); $fp = fopen ('newfile.txt', 'w'); foreach ($lines as $line) { list(,$name) = split($line, ","); list($fname, $lname) = split($name, " "); $line = str_replace($name, $fname . ",".$lname, $line); fwrite($fp,$line . "\n"); } fclose ($fp); ?> Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211509 Share on other sites More sharing options...
Barand Posted March 20, 2007 Share Posted March 20, 2007 When I ran my code with your data, newfile.txt contained 0000190147,ALEXANDER,PAUL,7247522388 ,EN,1,02/28/2007, 0000170212,ALTENBURG,SETH,7247584035 ,EN,3,02/28/2007, 0000200161,ALTERS,PAIGE,7247581035 ,EN,0,02/28/2007, 0000170141,ALTMAN,CHASE,7246744285 ,EN,2,02/28/2007, Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211513 Share on other sites More sharing options...
per1os Posted March 20, 2007 Share Posted March 20, 2007 lol I remember my old boss would have a sign that says: "It works on my machine, is not an excuse!" =) Just joshin ya barrand, but without the actual file, it is hard to say if there is something hidden. In reality, if he uploaded the datafile, than you could debug it, without that file, it is useless as it works on your machine =) Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211518 Share on other sites More sharing options...
Barand Posted March 20, 2007 Share Posted March 20, 2007 @Frost, If only you'd tested it, you would have seen several of these: Warning: split() [function.split]: REG_EMPTY in C:\Inetpub\wwwroot\test\noname7.php on line 8 and an output file containing 0000190147,ALEXANDER PAUL,7247522388 ,EN,1,02/28/2007, 0000170212,ALTENBURG SETH,7247584035 ,EN,3,02/28/2007, 0000200161,ALTERS PAIGE,7247581035 ,EN,0,02/28/2007, 0000170141,ALTMAN CHASE,7246744285 ,EN,2,02/28/2007, Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211524 Share on other sites More sharing options...
jschofield Posted March 20, 2007 Author Share Posted March 20, 2007 Ah Ill try it. I have the iinput as my data file. Sorry I had it switched. Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211525 Share on other sites More sharing options...
jschofield Posted March 20, 2007 Author Share Posted March 20, 2007 I did get that error. So you think my file wont work? Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211526 Share on other sites More sharing options...
per1os Posted March 20, 2007 Share Posted March 20, 2007 I am doing this from work with no php application or access installed =) It is fun to blind code, try this field: <?php $lines = file('iinput.txt'); $fp = fopen ('newfile.txt', 'w'); foreach ($lines as $line) { list(,$name) = split(",", $line); list($fname, $lname) = split(" ", $name); $line = str_replace($name, $fname . ",".$lname, $line); fwrite($fp,$line . "\n"); } fclose ($fp); ?> edited: fixed both splits. Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211532 Share on other sites More sharing options...
jschofield Posted March 20, 2007 Author Share Posted March 20, 2007 Ok guys this is what I have so far. I have two includes the importlibrary.inc.php is for logging. I use on most of my scripts. The other inclide is what Ihave from Frost. I think I maybe doing something wrong but not sure?? Script 2 is my include. Thanks again guys. I owe you guys a few drinks!! Script 1: <?php $inputfile = "C:/SMDIM/Data/schoolmessenger.txt"; $outputfile = "C:/SMDIM/Data/RIVERSIDE_BEAVER_CO_SD_ALL_STU.CSV"; $logfilename = "C:/SMDIM/Logs/RIVERSIDE_BEAVER_CO_SD_ALL_STU_LOG.TXT"; include'C:/SMDIM/Translators/importlibrary.inc.php'; $count = 0; if(!$in = fopen($inputfile, "r")) { wlogdie("Failed to open $inputfile for reading!"); }else{ wlog("Opened $inputfile for reading."); } if(!$out = fopen($outputfile, "w")) { wlogdie("Failed to open $outputfile for writing!"); }else{ wlog("Opened $outputfile for writing."); } while ($line = fgetcsv($in, 1000, ",")) { include'C:/SMDIM/Translators/include1.php'; $trimmedphone = str_replace(" ", "", $line[2]); $formattedlang = str_replace("EN", "ENGLISH", $line[3]); $tmpline = '"' . $line[0]; $tmpline .= '"' . ',' . '"' . $line[1]; $tmpline .= '"' . ',' . '"' . $trimmedphone; $tmpline .= '"' . ',' . '"' . $formattedlang; $tmpline .= '"' . ',' . '"' . $line[4]; $tmpline .= '"' . ',' . '"' . "Student" . '"'; $tmpline .= "\r\n"; $count++; fwrite($out, $tmpline); } if($count !== '0') { wlog("Finished the import with a total of $count records"); }else{ wlog("Failed to import data!"); } //Close the input and output files. fclose($in); echo "Closed the input file." . "\r\n"; fclose($out); echo "Closed the ouput file." . "\r\n"; ?> Script 2: <?php $lines = file('C:\SMDIM\Data\schoolmessenger.txt'); $fp = fopen ('C:\SMDIM\Data\schoolmessenger.txt', 'w'); foreach ($lines as $line) { list(,$name) = split(",", $line); list($fname, $lname) = split(" ", $name); $line = str_replace($name, $fname . ",".$lname, $line); fwrite($fp,$line . "\n"); } fclose ($fp); ?> Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211551 Share on other sites More sharing options...
per1os Posted March 20, 2007 Share Posted March 20, 2007 For easier viewing use [ code ] [ /code ] tags. Script 1: <?php $inputfile = "C:/SMDIM/Data/schoolmessenger.txt"; $outputfile = "C:/SMDIM/Data/RIVERSIDE_BEAVER_CO_SD_ALL_STU.CSV"; $logfilename = "C:/SMDIM/Logs/RIVERSIDE_BEAVER_CO_SD_ALL_STU_LOG.TXT"; include'C:/SMDIM/Translators/importlibrary.inc.php'; $count = 0; if(!$in = fopen($inputfile, "r")) { wlogdie("Failed to open $inputfile for reading!"); }else{ wlog("Opened $inputfile for reading."); } if(!$out = fopen($outputfile, "w")) { wlogdie("Failed to open $outputfile for writing!"); }else{ wlog("Opened $outputfile for writing."); } while ($line = fgetcsv($in, 1000, ",")) { include'C:/SMDIM/Translators/include1.php'; $trimmedphone = str_replace(" ", "", $line[2]); $formattedlang = str_replace("EN", "ENGLISH", $line[3]); $tmpline = '"' . $line[0]; $tmpline .= '"' . ',' . '"' . $line[1]; $tmpline .= '"' . ',' . '"' . $trimmedphone; $tmpline .= '"' . ',' . '"' . $formattedlang; $tmpline .= '"' . ',' . '"' . $line[4]; $tmpline .= '"' . ',' . '"' . "Student" . '"'; $tmpline .= "\r\n"; $count++; fwrite($out, $tmpline); } if($count !== '0') { wlog("Finished the import with a total of $count records"); }else{ wlog("Failed to import data!"); } //Close the input and output files. fclose($in); echo "Closed the input file." . "\r\n"; fclose($out); echo "Closed the ouput file." . "\r\n"; ?> Script 2: <?php $lines = file('C:\SMDIM\Data\schoolmessenger.txt'); $fp = fopen ('C:\SMDIM\Data\schoolmessenger.txt', 'w'); foreach ($lines as $line) { list(,$name) = split(",", $line); list($fname, $lname) = split(" ", $name); $line = str_replace($name, $fname . ",".$lname, $line); fwrite($fp,$line . "\n"); } fclose ($fp); ?> I think your problem lies here within script 1: while ($mline = fgetcsv($in, 1000, ",")) { include'C:/SMDIM/Translators/include1.php'; $trimmedphone = str_replace(" ", "", $mline[2]); $formattedlang = str_replace("EN", "ENGLISH", $mline[3]); $tmpline = '"' . $mline[0]; $tmpline .= '"' . ',' . '"' . $mline[1]; $tmpline .= '"' . ',' . '"' . $trimmedphone; $tmpline .= '"' . ',' . '"' . $formattedlang; $tmpline .= '"' . ',' . '"' . $mline[4]; $tmpline .= '"' . ',' . '"' . "Student" . '"'; $tmpline .= "\r\n"; $count++; fwrite($out, $tmpline); } changed $line to $mline to avoid conflicts with the included file. (mline = main line) What was happening was my script was overwriting the $line inside the while. Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211568 Share on other sites More sharing options...
jschofield Posted March 20, 2007 Author Share Posted March 20, 2007 Hey thanks Frost. The error im getting is from line 7 and 8 in the include1.php and it says undefined offset. Could it be screaming about the $name, $fname? Thanks again. Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211605 Share on other sites More sharing options...
per1os Posted March 20, 2007 Share Posted March 20, 2007 Honestly, I do not know why you included that into the loop, it should be outside the loop as it only needs to run once, unless the text filename changes in the loop. Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211607 Share on other sites More sharing options...
jschofield Posted March 20, 2007 Author Share Posted March 20, 2007 Nope filename doesn't change. I did move it though like it should be. Also I found the error I was getting on line 7. There was a , in $name. Still working on the line 8 issue. Thank you for all your help Frost110! Link to comment https://forums.phpfreaks.com/topic/43549-split-2-words-into-two-different-fields/#findComment-211631 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.