Jump to content

Split 2 words into two different fields


jschofield

Recommended Posts

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

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>

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);
?>

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);
?>

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,

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 =)

 

 

@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, 

 

 

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.

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);

?>

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.