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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

@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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.